B4R Question uLong to string changes value

sorex

Expert
Licensed User
Longtime User
Hello,

I wrote a small program years ago to read out our rfid badges.

Some recent cards showed up negative values.

No problem but as I need to enter the badges into another system aswell I want to normalize things.

So I switch everything from Long to uLong and all seemed fine until my eye noticed that the value is not equal afterall.

The code below displays the rfid tag and it's string variant.

Any idea how I can solve this? :)

B4X:
    Dim b() As ULong
    Dim tag As String
    b=bc.ulongsFromBytes(UID)
    rfidTag=b(0)
    Log(rfidTag)
    tag=rfidTag
    Log(tag)

output:
3397120948
3397121024.00
 

sorex

Expert
Licensed User
Longtime User
I currently use bc.hexFromBytes and do the convertion on the webserver side.

But a reason/solution might be usefull ofcourse.
 
Upvote 0

emexes

Expert
Licensed User
The code below displays the rfid tag and it's string variant.

output:
3397120948
3397121024.00

That looks exactly like what happens when a number is squeezed through a 23-24 bit mantissa of a single precision float.

eg
decimal 3397120948 = hexadecimal CA7BF3B4
decimal 3397121024 = hexadecimal CA7BF400

The bottom 8 bits have rounded to the nearest 00 boundary, ie from 3xx to 400.

It seems that the tag=rfidTag ULong to String conversion is being done via a float.

Perhaps tag=rfidTag.As(Double) might bypass the precision squeeze.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
Nice find. I didn't think about that.

Doesn't make sense to apply rounding but it explains why it suddenly added .00 to it.
 
Upvote 0
Top