iOS Question ToHexString

Similar threads

B4A Code Snippet [B4X] Hex string to number
Wish ToHexString
B4i Question ToHexString Issue
B4A Code Snippet [B4X] HexToColor and ColorToHex
B4A Library [B4X] [XUI] HexMenu

jo1234

Member
Licensed User
Hi

I would like to convert a String ("1011") to an Int and finally to a Hex ("B").

In B4A I use
B4X:
Dim sHexPattern As String
sHexPattern = Bit.ToHexString(Bit.ParseInt("1011"))
What would be the code in B4i?

Thanks a lot!
 

Erel

Administrator
Staff member
Licensed User
You can use this code (in B4A as well):
B4X:
Dim bc As ByteConverter
Log(bc.HexFromBytes(Array As Byte(Bit.ParseInt("1011", 2)))) 'will print 0B.
bc.HexFromBytes is in most cases better than Bit.ToHexString (which is not available in B4i) as it writes each byte as two characters adding 0 if needed.

You can add .SubString(1) if you want to get B instead of 0B.
 

mrossen

Active Member
Licensed User
Hi,

B4X:
Dim bc As ByteConverter
Log(bc.HexFromBytes(Array As Byte("140")))) 'will print 8C.
B4X:
Dim bc As ByteConverter
Log(bc.HexFromBytes(Array As Byte("7440")))) 'will print 10.
The last example should return 1d10.

The problem is that the number can not be higher than 255 for the byte.

How do I convert number higher than 255?
Mogens
 

mrossen

Active Member
Licensed User
Hi again,

Now I got it to work, but is this the right way to do it?

B4X:
 Log(bc.HexFromBytes(bc.IntsToBytes(Array As Int("7440")))) 'will print 00001d10
Mogens
 
Top