Hex string to Int

Armoured

Member
Licensed User
Longtime User
Hi,
In Java I can write:

B4X:
String hex = "2A"  
int intValue = Integer.parseInt(hex, 16);

And in Basic4Android?
 

Beja

Expert
Licensed User
Longtime User
Thanks Armoured for the tip!
do you know b4a function for binary to hex?
I have the vb6: hexbyte = bin2hex("10110010")
 
Upvote 0

Stulish

Active Member
Licensed User
Longtime User
i have created a sub routine to convert binary to decimal:

B4X:
Sub binTodec(binVal As String) As Int
Dim i As Int, v As Int, dec As Int 
i = binVal.Length
v = 1
For pos = i To 1 Step -1
    If binVal.SubString2(pos-1, pos) = "1" Then dec = dec + v
v = v * 2
Next 
Return dec
End Sub

then you can use:

B4X:
Bit.ToHexString(binTodec("10110010"))

or similar
 
Upvote 0

Armoured

Member
Licensed User
Longtime User
Thanks Armoured for the tip!
do you know b4a function for binary to hex?
I have the vb6: hexbyte = bin2hex("10110010")

I think you want this:
B4X:
Sub Bin2Hex(bnumber As String) As String
  Return Bit.ToHexString(Bit.ParseInt(bnumber,2))
End Sub

Then you can use:
B4X:
Dim hexbyte as String
hexbyte = Bin2Hex("10110010")
;)
 
Last edited:
Upvote 0

Stulish

Active Member
Licensed User
Longtime User
Nice :)
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Armoured,
Did you test the above sub? in my machine it is saying nonconvertible type.
didn't touch your code, but tried to put the returned string in an edittext.
Thanks in advance for more light on this.
 
Upvote 0

Armoured

Member
Licensed User
Longtime User
Armoured,
Did you test the above sub? in my machine it is saying nonconvertible type.
didn't touch your code, but tried to put the returned string in an edittext.
Thanks in advance for more light on this.

Yes I have tested my code but you can control for yourself with this:
B4X:
Msgbox("10110010 = " & Bin2Hex("10110010"),"Binary to Hex")
 

Attachments

  • Bin2Hex.png
    Bin2Hex.png
    14.2 KB · Views: 688
Last edited:
Upvote 0

Similar Threads

Top