Convert binary string to integer

I have a 4 byte string that was read from a file… its binary value is “00 00 00 0C”…

How can I convert that into the integer 12?


s = “&h” + s
i = s.ToInteger

Presumably that depends on whether the OP has written out his hex string (not binary) exactly as received or has added the spaces for clarity.

Kem… I tried that but get 0 and not 12… s becomes “&h” with 4 spaces.

The text in the debugger is 4 blank spaces.

Tim… I added the spaces between the 00 for clarity.

Oh, those are the bytes.

var mb as MemoryBlock = s
mb.LittleEndian = false
i = mb.Int32Value(0)
1 Like

or else :wink: :

var s as String =chr(0)+chr(0)+chr(0)+chr(12)

var x As integer = asc(s.Right(1))+(asc(s.Mid(3,1))*256)+(asc(s.Mid(2,1))*65536)+(asc(s.left(1))*16777216)

Thanks… Kem’s solution works well and seems the easiest.

1 Like

And if such 32 bit value is an int and not an uint, also produces correct results for negative values.