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
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:
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
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