iOS Question ToHexString

jo1234

Active Member
Licensed User
Longtime 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

B4X founder
Staff member
Licensed User
Longtime 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.
 
Upvote 0

mrossen

Active Member
Licensed User
Longtime 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
 
Upvote 0

mrossen

Active Member
Licensed User
Longtime 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
 
Upvote 0
Top