B4J Question Converting through bases

fabero

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

Expert
Licensed User
Longtime User
Looks like ParseInt uses signed values, so 0x7fffffff is the maximum that will fit in an int.
 
Last edited:
Upvote 0

fabero

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

Erel

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

fabero

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

fabero

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