B4J Question Converting through bases

Discussion in 'B4J Questions' started by fabero, May 24, 2015.

  1. fabero

    fabero Active 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"
     
  2. Daestrum

    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: May 24, 2015
  3. fabero

    fabero Active 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?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Try this code:
    Code:
    Sub AppStart (Args() As String)
       
    Log(HexToLong("FF"))
       
    Log(HexToLong("FFFFFFFFFF"))
    End Sub

    Sub HexToLong(hex As StringAs Long
       
    Dim bc As ByteConverter
       
    If hex.Length > 16 Then
         hex = hex.SubString2(
    016)
       
    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
     
  5. fabero

    fabero Active Member Licensed User

    I obtain before the error:
    255
    1099511627775
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Which error?

    The values are correct.
     
  7. fabero

    fabero Active 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?
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    I'm getting the same error in B4A.
     
  9. fabero

    fabero Active Member Licensed User

    In the other thread that I open for my mistake seems not. Awh.. So thanks a lot Erel as usual.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice