Android Question [SOLVED] Question about "ParseInt"... or bad use of it

Discussion in 'Android Questions' started by Luca Rinaldoni, Jul 12, 2018.

  1. Luca Rinaldoni

    Luca Rinaldoni Member Licensed User

    Hi,
    I try to read UID from a tag RFID, type MIFARE Ultralight, with "Ultrareader" sample that I've found in this site.

    The result of two reads are:

    TAG RFID 'Ultralight'
    #1
    Out: 04F62F55026D3F84D448000000000000
    UID: 026D3F84

    #2
    Out: 0454D20ACA6C3F801948000000000000
    UID: CA6C3F80


    The UID variable contains value of identifier that I wont convert into a number.

    I had reach to parse UID of sample #1 into 'i' variable, and I obtain '40714116'.
    I using this routine:

    Try
    Dim i As Int
    i = Bit.ParseInt(strData, 16)

    Log(i)
    Catch
    Log("Failed ParseInt: " & LastException)
    End Try

    The #2 case raise an exception: java.lang.NumberFormatException: Invalid int: "CA6C3F80".
    I suppouse that the value of second sample return a number more big then 'int'.

    Can I convert (or parse) the value on Long? Is there a function?


    Thanks in advance
    ;)
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Please use [code]code here...[/code] tags when posting code.

    4 bytes represent an int. However Bit.ParseInt will only work with positive numbers.

    Use this code instead:
    Code:
    Dim bc As ByteConverter
    Dim id As Int = bc.IntsFromBytes(bc.HexToBytes("CA6C3F80"))(0)
     
    Luca Rinaldoni likes this.
  3. Luca Rinaldoni

    Luca Rinaldoni Member Licensed User

    Thanks Erel!
    Your sample works, but I need a positive number; in C# I used Convert.Int64 like this:

    Code:
    Convert.Int64("CA6C3F80"16) => Result: 3396091776
    Can I obtain a positive number with a function of ByteConverter?
    I try to use this:
    Code:
    Dim idLong As Long = bc.LongsFromBytes(bc.HexToBytes(strData))(0)
                
    Log(idLong)
    but raised an exception: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0.


    Thanks for your help...
     
  4. JordiCP

    JordiCP Well-Known Member Licensed User

    You are casting an int to long because bc.longsFromBytes fails, but as the int (signed in B4A) is already negative, the long will also be because of the casting
    Add the bottom lines
    Code:
    Dim bc As ByteConverter
    Dim id As Long = bc.intsFromBytes(bc.HexToBytes("CA6C3F80"))(0)
    Log(id)  '  -898875520   (in fact it is 0xFFFFFFFFCA6C3F80)
    If id<0 Then id=id+4294967296   '<-- we are adding 0x100000000
    Log(id)  '  3396091776
     
    Erel, DonManfred and Luca Rinaldoni like this.
  5. Luca Rinaldoni

    Luca Rinaldoni Member Licensed User

    Thanks for your help, JordiCP! :)
    Works fine.
    I saved a lot of time.
     
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