# Binary string to int conversion

## 8 posts in this topic

Hi,

I'm trying to convert a binary string format to integer using Q03UDE CPU.

String data : '1001'

Expected Result :  9

Kindly help.

##### Share on other sites

You don't have to do anything. This is just interpretation of data.

De word device containing the data will look like when monitoring in Binary format 0000.0000.0000.1001

If you monitor Dec in you will see 9
If you monitor Hex in you will see 9

If it is string data then you can use STR_TO_INT

##### Share on other sites

Hi Gambit,

I tried STR_TO_INT function, however the result is '10100'.

##### Share on other sites

I am confused now. Do you have a String (ascii) value or Binary?

##### Share on other sites

Hi,

I have a string variable named stringData with a value of '1001' binary format.. I want to convert it to a decimal value of 9 . I found a solution for this using some manipulation.. however, I am wondering if there's a direct conversion for this..

please check the attached image.. I am using ST program.

##### Share on other sites

You should explain a bit more why and what you are doing. Plus, that line of code explains nothing except the fact that your are using a string in almost "binary" format what you want to convert. There are no logical reasons (the way I see it) why you would ever interpret stringdata 1001 into decimal 9...

How did you get stringdata 1001? What are you going to do with it? Why do you need it converted? What's the purpose?

##### Share on other sites

i agree, using string to represent numeric values is less than ideal.

but if that's what you have, use a loop and go through each string character,

something like this (sample pseudo code):

```
INT N, I
N:=0
FOR I:=1 to stringData.LEN
N:=N+N
if stringData[I]=='1' then
N:=N+1
endif
NEXT```