B4R Question 'NumberFormat' rounding problem

Discussion in 'B4R Questions' started by Mostez, Aug 16, 2018.

  1. Mostez

    Mostez Active Member

    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
    Code:
    private Sub ReadRFID(Buffer() As Byte)
        
        
    Dim tmpBuffer (8As 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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    NumberFormat receives a Double (same as Float in B4R). So it can have some small inaccuracies in large numbers.

    Why do you need it?
     
  3. Mostez

    Mostez Active Member

    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?
     
  4. tigrot

    tigrot Well-Known Member Licensed User

    Use a larger target, like 12 digits, then truncate.
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    Sub Process_Globals
       
    Public Serial1 As Serial
       
    Private PadResult(10As 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
     
    Peter Simpson and Mostez like this.
  6. Mostez

    Mostez Active Member

    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!

    Code:
    Dim strCardArray(10As Byte = Pad(BC.ULongsToBytes(Array As ULong(Int_Number)))
    and I passed it like this and I've got decimal point result
    Code:
    Dim tmp_str As String = Int_Number
    Dim strCardArray(10As Byte = Pad(tmp_str.GetBytes)
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    What is the output of this code:
    Code:
    Dim Number As Long = 9040399
    Log(NumberFormat(Number,10,0))
    It prints 0009040399 here.
     
  8. Mostez

    Mostez Active Member

    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
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Try this:
    Code:
    Dim Number As ULong = 9040399
    PadLong(Number)
    Log(PadResult)
    Code:
    Sub PadLong(Input As ULong)
       
    Dim c As ULong = 1
       
    For i = 0 To PadResult.Length - 1
           
    Dim i2 As ULong = Input / c
           PadResult(PadResult.Length - 
    1 - i) = Asc("0") + (i2 Mod 10)
           c = c * 
    10
       
    Next
    End Sub
     
    Mostez and Peter Simpson like this.
  10. Mostez

    Mostez Active Member

    sorry for late reply, I'm on vacation and will back to work within few days
     
  11. Mostez

    Mostez Active Member

    it works OK now, thank you so much Erel

    Reading | ################################################## | 100% 0.82s
    ********************* PROGRAM STARTING ****************
    AppStart
    0009040399
     
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