# Convert binary string to integer

**URL:** <https://forum.xojo.com/t/convert-binary-string-to-integer/73732>\
**Category:** General\
**Created:** [December 17, 2022, 5:13pm UTC](https://forum.xojo.com/t/convert-binary-string-to-integer/73732 "2022-12-17T17:13:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jim\_Meyer](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@Jim\_Meyer](https://forum.xojo.com/u/Jim_Meyer)\
**Post date:** [December 17, 2022, 5:13pm UTC](https://forum.xojo.com/t/convert-binary-string-to-integer/73732/1 "2022-12-17T17:13:26Z")

</div>

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?

Thanks,  
Jim

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [December 17, 2022, 5:22pm UTC](https://forum.xojo.com/t/convert-binary-string-to-integer/73732/2 "2022-12-17T17:22:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [December 17, 2022, 5:25pm UTC](https://forum.xojo.com/t/convert-binary-string-to-integer/73732/3 "2022-12-17T17:25:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jim\_Meyer](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@Jim\_Meyer](https://forum.xojo.com/u/Jim_Meyer)\
**Post date:** [December 17, 2022, 5:46pm UTC](https://forum.xojo.com/t/convert-binary-string-to-integer/73732/4 "2022-12-17T17:46:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jim\_Meyer](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@Jim\_Meyer](https://forum.xojo.com/u/Jim_Meyer)\
**Post date:** [December 17, 2022, 5:47pm UTC](https://forum.xojo.com/t/convert-binary-string-to-integer/73732/5 "2022-12-17T17:47:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [December 17, 2022, 5:55pm UTC](https://forum.xojo.com/t/convert-binary-string-to-integer/73732/6 "2022-12-17T17:55:28Z")

</div>

Oh, those are the bytes.

```auto
var mb as MemoryBlock = s
mb.LittleEndian = false
i = mb.Int32Value(0)

```

---

<div class="post-metadata">

**Author:** ![Pawel\_Soltysinski](https://forum.xojo.com/user_avatar/forum.xojo.com/pawel_soltysinski/32/23522_2.png) [@Pawel\_Soltysinski](https://forum.xojo.com/u/Pawel_Soltysinski)\
**Post date:** [December 17, 2022, 6:32pm UTC](https://forum.xojo.com/t/convert-binary-string-to-integer/73732/7 "2022-12-17T18:32:44Z")

</div>

or else 😉 :

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![Jim\_Meyer](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@Jim\_Meyer](https://forum.xojo.com/u/Jim_Meyer)\
**Post date:** [December 17, 2022, 7:14pm UTC](https://forum.xojo.com/t/convert-binary-string-to-integer/73732/8 "2022-12-17T19:14:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [December 18, 2022, 12:34pm UTC](https://forum.xojo.com/t/convert-binary-string-to-integer/73732/9 "2022-12-18T12:34:20Z")

</div>

> [@Jim\_Meyer](#):
>
> Kem’s solution works well and seems the easiest.

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