B4J Question Converting through bases

fabero

Member
Licensed User
I've a textbox text value

So I use

Dim i As Int
i = Bit.ParseInt(textfield.Text,16)

If the text field has 3412CDAB it works, if it has FFFFFFFF I gave

java.lang.NumberFormatException: For input string: "FFFFFFFF"
 

Daestrum

Well-Known Member
Licensed User
Looks like ParseInt uses signed values, so 0x7fffffff is the maximum that will fit in an int.
 
Last edited:

fabero

Member
Licensed User
But, for my mistake I post in B4A forum, and there look like that code works. So perhaps it is a "bug" ?

There isn't a way to convert that?
 

Erel

Administrator
Staff member
Licensed User
Try this code:
B4X:
Sub AppStart (Args() As String)
   Log(HexToLong("FF"))
   Log(HexToLong("FFFFFFFFFF"))
End Sub

Sub HexToLong(hex As String) As Long
   Dim bc As ByteConverter
   If hex.Length > 16 Then
     hex = hex.SubString2(0, 16)
   Else
     Dim sb As StringBuilder
     sb.Initialize
     sb.Append(hex)
     Do While sb.Length < 16
       sb.Insert(0, "0")
     Loop
     hex = sb.ToString
   End If
   Dim data() As Byte = bc.HexToBytes(hex)
   Return bc.LongsFromBytes(data)(0)
End Sub
 

fabero

Member
Licensed User
I add only your code, and not modify my apps. Yes is correct, so I can use your sub.
But, why seems that in B4A there isn't the same problem?
 

fabero

Member
Licensed User
In the other thread that I open for my mistake seems not. Awh.. So thanks a lot Erel as usual.
 
Top