Android Question Bit.ParseInt("010100......10101010", 2)

peacemaker

Expert
Licensed User
Longtime User
HI, All

Result is java.lang.NumberFormatException: For input string: "0101000110101011010101011010100001010101101010100100001010101010"

What's wrong here?

UPD:
Oops, sorry, too big int, i see now.
But how to convert ?
 

emexes

Expert
Licensed User
Or do this:
B4X:
Sub BigBinary(S As String) As Long
  
    Dim N As Long = 0
  
    For I = 0 To S.Length - 1
        Select Case S.CharAt(I)
            Case "0"  : N = N + N
            Case "1"  : N = N + N + 1
            Case Else : Exit    'reached end of number
        End Select
    Next
  
    Return N
  
End Sub
Curiously, this real-programmer approach is probably simpler than massaging the binary into hex.
 
Upvote 0

emexes

Expert
Licensed User
Also curious is that it doesn't take much to adapt it to using any digit characters and any base:
B4X:
Sub BigAnything(S As String, Digits As String) As Long
 
    Dim N As Long = 0
 
    For I = 0 To S.Length - 1
        Dim ThisDigit As Int = Digits.IndexOf(S.CharAt(I))
        If ThisDigit < 0 Then
            Exit    'invalid digit = reached end of number
        End If
   
        N = N * Digits.Length + ThisDigit
    Next
 
    Return N
 
End Sub

Log(BigAnything("1232", "0123456789"))    'decimal
Log(BigAnything("1010", "01"))    'binary
Log(BigAnything("33", "01234567"))    'octal
Log(BigAnything("1B", "0123456789ABCDEF"))    'hexadecimal
Log(BigAnything("FFFF", "0123456789ABCDEF"))
Log(BigAnything("YIKES", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))    'hexavigesimal
Log(BigAnything("BASE36", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"))    'hexatridecimal
 
Last edited:
Upvote 0
Top