Android Question Hex string to Int

Steve-h

Member
I can convert int to hex using
B4X:
    Dim input As Int = 65535
    Dim output As String
    output = Bit.ToHexString(input)
    Log(output)

Please can someone suggest a way of converting back from Hex to string and explain it in a simplistic way so I can understand.

I have tried various examples from the forum using ByteConverter or Bit.ParseInt but without success. Most of my attempts end up with a Java exception, or don't compile.

Presumably it is a two stage process, first converting the string to bits and then to int.
 

Steve-h

Member
After puzzling over this for most of the day I have solved it myself. What I didn't find explained in a way I understood was the 16 which relates to the number base you are converting from.

B4X:
result =Bit.ParseInt("FFFF",16)
 
Upvote 0

MikeSW17

Active Member
Licensed User
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
You need to modify the above slightly if you want to convert a css hexadecimal color to b4x color int.

B4X:
Sub test
    Log(xui.Color_Red)
    Dim javaColor As String = "#ff0000"        'red
    
    Dim b4xcolor As String = "ff" & javaColor.SubString(1)    'remove # and add the transparent component
    Log(StringToInt(b4xcolor))
End Sub

Sub StringToInt(Str As String) As Int
    Dim converter As ByteConverter
    Dim ii() As Int = converter.IntsFromBytes(converter.HexToBytes(Str))
    Return ii(0)
End Sub
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
test best:

a routine taken from a conversion function I have, check it out.

library ByteConverter

B4X:
    Dim Number As String = 65535
    Dim BC As ByteConverter
   
    Dim sHexDecimal As String = BC.HexFromBytes(BC.IntsToBytes(Array As Int(Number.As(Int))))
    Log(sHexDecimal)
   
    Dim IntArray() As Int = BC.IntsFromBytes(BC.HexToBytes(sHexDecimal))
    Log(IntArray(0))

1644626893882.png
 
Upvote 1
Top