# B4J QuestionBinary String to Hex

##### Member
Hi everyone,

hoping i can get some advice, I have a String (which is binary) but need a hex number is there any nice way of doing this?

binary is off unknown length... but minimum 8

cheers

#### Erel

Staff member
Please post an example of the input string and the expected output.

##### Member
Binary 1000000000000000000000000000000000000000000000000000
hex 8000000000000

The caveat to this is the binary will be at least 8 bits but up to an unknown length....

#### amykonio

##### Member
This binary string will always contain a positive number or it can contain also a negative? It will be of a defined length (and zero filled if necessary)? How this binary number was created?

##### Member
hi, yep it will always be 0 or positive! its created by string concatination on if statements
B4X:
``````Sub AlarmActive (AlMask As String)
If Not(Main.sql1.IsInitialized) Then
Log("SQL1 Failed")
Return
End If
'    Dim count As Int
'    count = 0
Dim RS As ResultSet = Main.sql1.ExecQuery("SELECT * FROM DeviceStatus INNER JOIN DeviceLive ON DeviceStatus.DeviceSN = DeviceLive.DeviceSN ")
Do While RS.NextRow
Log("DevSN; " & RS.GetInt("DeviceSN") &  "   EmStat; " & RS.GetInt("EmStatus"))
If RS.GetInt("EmStatus")  < "4" Then
Else If RS.GetInt("EmStatus") = "4" Then
Else If RS.GetInt("EmStatus") = "5" Then
Else If RS.GetInt("EmStatus") = "6" Then
Else if RS.GetInt("EmStatus") = "7" Then
Else
Log("error")
Log(RS.GetInt("EmStatus"))
End If
Loop

'    Dim byteAl() As Byte = AlMask.GetBytes("UTF8") 'convert to bytes
'    Log(BytesToString(byteAl, 0, byteAl.Length, "UTF8")) 'convert to string
'    Dim b(1) As Byte
'    Dim msg As String
'    For i = 0 To byteAl.Length - 1
'        b(0) = byteAl(i)
'        msg = conv.HexFromBytes(b)
'        Log(msg)
'    Next
'
'

End Sub``````
however it will not be a defined length, and will often return something like; 0001000000000000000100000000

#### DonManfred

##### Expert
something like; 0001000000000000000100000000
NEVER!
Based on
"00000000"
"00000000" + "0001" for ex.

00000000000110000010 is made of "00000000"+"0001"+"1000"+"0010" in case of 3 results from the database.

Last edited:

#### amykonio

##### Member
So the length will always be a divided by 4 (to check if we have a valid string)? And you need to convert the string to a hex for each set of 4 bits or for the full length of bits (I guess it will be the second)...

#### Erel

Staff member
Not tested too much:
B4X:
``````Sub BinaryToHex (Binary As String) As String
Do While Binary.Length Mod 4 <> 0
Binary = "0" & Binary
Loop
Dim res As StringBuilder
res.Initialize
For i = 0 To Binary.Length - 1 Step 4
res.Append(Bit.ToHexString(Bit.And(0xf, Bit.ParseInt(Binary.SubString2(i, i + 4), 2))))
Next
Return res.ToString
End Sub``````

##### Member
hah yes good point Don! So the total needs to be converted to hex!, sorry i should of been clearer on this!

#### emexes

##### Expert
created by string concatenation on if statements
B4X:
``````...
If RS.GetInt("EmStatus")  < "4" Then
Else If RS.GetInt("EmStatus") = "4" Then
Else If RS.GetInt("EmStatus") = "5" Then
Else If RS.GetInt("EmStatus") = "6" Then
Else if RS.GetInt("EmStatus") = "7" Then
Else
...``````
Is there some reason for binary?

Why not just send the values directly, eg: 0, 1, 2 , 4 or 8?

The repeated calls to RS.GetInt("EmStatus") are mildly painful, too. Is there a reason it can't be done just once, and the result held in a temporary Int? And then compared to Int rather than String literals.

The AlMask parameter seems to be unused. And whilst Subs have a default return type of String, which happily matches what you are returning... it might be better to codify this so that there is no ambiguity, eg:
B4X:
``Sub AlarmActive () As String``
(assuming the AlMask parameter is not needed)

Last edited:

##### Member
Thankyou @emexes for the recommendation of setting the sub to return string. The reason for the binary is I'm interfacing with a system that assigns lines in a specific way for example the first digit in the binary represents line 1, the second line 2 the third line 3 and so on. Annoyingly because of this I need all the 0s as they are, the final binary then needs to be converted as a whole to hex. Sadly i have no ability to change the way i get to interface with this device, hence the strange binary hex combo!

#### emexes

##### Expert
Thankyou @emexesSadly i have no ability to change the way i get to interface with this device
If it's any consolation: we've all been there, wondering what the heck the original designer was thinking (or smoking?)

##### Member
Sorry to be a pain guys, I'm still floundering here, with a string of binary e.g. 000000000000000000000010000000000000000000000000 I need to convert the whole thing to hex e.g. 2000000 I've used @Erel 's code further up but get 2000000 instead from my binary string? I might be being very thick here but does that step through and convert each 4 digits or the whole string?

#### Erel

Staff member
I need to convert the whole thing to hex e.g. 2000000 I've used @Erel 's code further up but get 2000000 instead from my binary string?
I don't understand the problem.

##### Member
So using the code snippet you kindly provided I successfully turned my string into hex however it was 2 decimal places out? 2000000 instead of 200000000.
I was wandering whether this is as it converts each block of 4 digits as opposed to the string as a whole? if so how would I change it?

#### Daestrum

##### Well-Known Member
If you want it to treat it as 2000000 hex you need to prepend 0x to the string.
ie, 0x2000000

If you use the string without the 0x it will assume its an int

##### Member
@Daestrum yes forgot about that thankyou!

#### Erel

Staff member
Every 4 binary digits = 1 hex digit.

Count the number of zeroes after 10. There are 24 zeroes. This means that the number ends with 2,000,000.

Last edited:

#### emexes

##### Expert
with a string of binary e.g. 000000000000000000000010000000000000000000000000 I need to convert the whole thing to hex e.g. 2000000
What are you doing with the hex number 2000000? Code would be useful.

#### emexes

##### Expert
``````binary: 0000,0000,0000,0000,0000,0010,0000,0000,0000,0000,0000,0000