Hexadecimal string to a long number

Galbas

Member
Licensed User
Longtime User
Hi!
How can I convert "0xFFFFFFFF" (string) to a long number. I need show this number. I have a label called lblDesl and I need to put the number in this label.
Thanks,
Galbas
 

Galbas

Member
Licensed User
Longtime User
Hi Erel!
Thank you, but I still having problems. My code is:
Dim D As Int
Dim msgRxDesl As String

msgRxDesl = "0x" & "FFFFFFFF"
D = Bit.ParseInt(msgRxDesl,16)
lblDesl.Text = NumberFormat(D,1,0)

The screen shows me an error:
D = Bit.ParseInt(msgRxDesl,16)

java.lang.NumberFormatException: unable to parse '0XFFFFFFFF' as integer

What is wrong?
Thnaks,
Galbas.
 
Upvote 0

joseluis

Active Member
Licensed User
Longtime User
The number is too big.

Remember that ParseInt returns an Int. (4bytes signed: -2147483648 to 2147483647)
In hexadecimal that is from -80000000 to 7FFFFFFF. Anything greater and it will fail.

Also: You must not include the leadign "0x" before the hex number:
B4X:
msgRxDesl = "7FFFFFFF" ' This works
 
Last edited:
Upvote 0

rossati

Active Member
Licensed User
Longtime User
Hello

This can be useful for convert an 8 byte Hexadecimal in an integer for colors purpose i.e. it works as colors.ARGB method:
B4X:
Sub decodeColor(C As String) As Int
Dim Match As Matcher = Regex.Matcher("[\daAbBcCdDeEfF]{8}", C)    ' eight exadecimal digits
If Not(Match.Find) Then Return -16777216    ' opaque black
If (C.SubString2(0,1)).compareto("7") > 0 Then
    Return - Power(2,31) + Bit.ParseInt("01234567".CharAt(Bit.ParseInt(C.SubString2(0,1),16)-8) & C.SubString(1),16)
End If
Return Bit.ParseInt(C,16)
End Sub

best regards

John Rossati
 
Upvote 0

Tron

Member
Licensed User
Longtime User
Hi.
Having the same problem, but a more efficient solution I think.

B4X:
Sub ConvertHexStringToInt (Tx As String ) As Int
Dim i As Int
Dim Ty As String

' Guarantee the required string length of 8  e.g. DAB89 => 000DAB89
i = 8 - Tx.Length

' ERROR string too Long (or write your own rules please)
If i < 0 Then
    Return -1
End If

' if string is shorter -> fill string with leading Zeros
If i > 0 Then
   Ty = "00000000".SubString2(0,i) & Tx
Else
   Ty = Tx
End If


' And now lets go binary ;-)
' convert the higher part first
i = Bit.ParseInt (Ty.SubString2 (0,4),16)
' correct the value (lower 16 bits are set to zero by the shift operation)
i = Bit.ShiftLeft (i,16)
' add the lower part and combine
i = Bit.OR (i,Bit.ParseInt (Ty.SubString2 (4,8),16))

Return i

End Sub

greets Jens
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…