B4R Question 'NumberFormat' rounding problem

Mostez

Well-Known Member
Licensed User
Longtime User
Hello;
I read RFID tags by RDM6300 module, everything went fine years ago, but recently I've got strange error. I send tag number to serial port. I use NumberFormat to format tag number in 10 digits.
the problem:
this tag number '0009040399' rounded to '0009040400'. listed some read tags with no problems;
i noticed that the tag number starts with '90' unlike others!
here is the code i use to read tags
B4X:
private Sub ReadRFID(Buffer() As Byte)
    
    Dim tmpBuffer (8) As Byte
    Dim Str_HexNumber As String
    Dim Int_Number As ULong
    tmpBuffer= BC.SubString2(Buffer,3,11) 'get card bytes only
    Str_HexNumber = BC.StringFromBytes(tmpBuffer)
    Int_Number = Bit.ParseInt(Str_HexNumber, 16) 'convert hex to decimal string
    Log(NumberFormat(Int_Number,10,0)) ' rounded to 0009040400
    Log(Int_Number) ' no problems 9040399
End Sub
0001467324
0005956339
0001608891
0001441980
0005920566
0003136545
0001390048
0006055974
 

Mostez

Well-Known Member
Licensed User
Longtime User
I need it to format tag number in 10 digits format, just to add leading zeros to number. is there any other accurate method to do so?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private PadResult(10) As Byte
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   Pad("920566")
   Log(PadResult)
   Pad("1")
   Log(PadResult)
   Pad("1234567890")
   Log(PadResult)
End Sub

Sub Pad(Input() As Byte)
   For i = 0 To PadResult.Length - 1
       If Input.Length - PadResult.Length + i < 0 Then
           PadResult(i) = Asc("0")
       Else
           PadResult(i) = Input(Input.Length - PadResult.Length + i)
       End If
   Next
End Sub
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
many thanks Erel, a last question please, tag id is a Ulong variable, how can I pass it to Pad sub as array of bytes i.e. "9040399"? I used this code but i get array length error it seems that it passes 4 bytes of Ulong, not string represented Ulong!

B4X:
Dim strCardArray(10) As Byte = Pad(BC.ULongsToBytes(Array As ULong(Int_Number)))

and I passed it like this and I've got decimal point result
B4X:
Dim tmp_str As String = Int_Number
Dim strCardArray(10) As Byte = Pad(tmp_str.GetBytes)
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
I ran it on MEGA 2560

Reading | ################################################## | 100% 0.79s
avrdude: verifying ...
avrdude: 5902 bytes of flash verified
avrdude done. Thank you.
********************* PROGRAM STARTING ****************
AppStart
0009040400
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
it works OK now, thank you so much Erel

Reading | ################################################## | 100% 0.82s
********************* PROGRAM STARTING ****************
AppStart
0009040399
 
Upvote 0
Top