Shop OBEX P1 Docs P2 Docs Learn Events
Parsing a byte value into three ASCII numeric characters — Parallax Forums

Parsing a byte value into three ASCII numeric characters

John KauffmanJohn Kauffman Posts: 653
edited 2008-03-31 04:14 in General Discussion
Parsing a byte value into three ASCII numeric characters

I got some good help on STAMP forum from PeterH and Mark Green with a routine to parse a byte value into thee characters that represent the value in decimal. The code they offered works fine in PBASIC.
·
I tried porting to SX/B (mainly change to one math operation per line). I'm testing with input values of 000, 023, 123 and 255. The result is that the 100's digit comes through fine but the 10's and unit digit is always zero. Any help appreciated.
·
LCD_Byte_Dec3:
' converts byte value into three ASCII characters
' displays those characters as dec numbers
' only good with byte-size input. Needs three temp vars declared
' Temp1 = dividend· Temp2 = divisor·· Temp3 = quotient
' remainder becomes new dividend (Temp1)
·
Temp1 = __PARAM1························ ' save the byte into the dividend
Temp2 = 100······························································ ' set the divisor for first loop
for idx1 = 1 to 3·································· ' run the loop once per digit
··········· Temp3 = Temp1/Temp2·········· ' get quotient by dividend/divisor
··········· Temp1 =__remainder·· ' save remainder for dividend of next loop
··········· Temp3=Temp3+48····························· ' convert quotent to ASCII
··········· lcd_byte Temp3·································· ' print ASCII quotent
··········· Temp2 = Temp2/10················ ' get divisor ready for next loop
next
RETURN

Comments

  • JonnyMacJonnyMac Posts: 9,216
    edited 2008-03-29 18:45
    I suspect, John, that one or more of your temporary variables is being clobbered by your LCD_BYTE subroutine but you don't show it so I can't be sure. Yesterday I was working on a demo program for Parallax's new MLX90614 temperature sensor and needed straight DEC (variable width) output. I've pulled the necessary parts from that as well as the DEC3 that you want to do; see the program that's attached as I think it will help you.

    Update: Added routine to right-justify (with leading spaces) within a 3-column field.

    Post Edited (JonnyMac) : 3/30/2008 12:45:43 AM GMT
  • John KauffmanJohn Kauffman Posts: 653
    edited 2008-03-29 19:02
    Exactly right. Temp1 & Temp2 were also used in Lcd_Byte. I was trying to follow the approach in some exmples of re-using variables, but managed to learn the danger of that.
    I haven't read your code yet, but here is what I have after substituting for conflicting variables.

    LCD_Byte_Dec3:
    ' converts byte value into three ASCII characters
    ' displays those characters as dec numbers
    ' only good with byte-size input
    ' remainder becomes new dividend (Temp1)
    dividend = __PARAM1 ' save the byte into the dividend
    divisor = 100 ' set the divisor for first loop
    for idx1 = 1 to 3 ' run the loop once per digit
    quotient = dividend/divisor ' get quotient by dividend/divisor
    dividend =__remainder ' save remainder for dividend of next loop
    quotient=quotient + 48 ' convert quotent to ASCII
    lcd_byte quotient ' print ASCII quotent
    divisor = divisor/10 ' get divisor ready for next loop
    next
    RETURN
  • Sens-a-DatSens-a-Dat Posts: 44
    edited 2008-03-30 21:31
    John,

    Maybe the following will help. I just sat down to do this, and it appears to work correctly. I am not saying this is exactly what you want, but I believe it may give you insight as to how to make yours work. ...I hope.

    Gary



    ' -------------------------------------------------------------------------
    ' Device Settings
    ' -------------------------------------------------------------------------
    
    DEVICE          SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
    FREQ            4_000_000
    
    ' -------------------------------------------------------------------------
    ' Variables
    ' -------------------------------------------------------------------------
    IByte var byte                 ' byte to be converted
    HByte var byte                 ' bytes to represent Hundreds digit
    TByte var byte                 ' bytes to represent Tens digit
    SByte var byte                 ' bytes to represent Singles digit
    
    ' =========================================================================
      PROGRAM Start
    ' =========================================================================
    
    Start:
      
    
    Main:
    
    IByte = 123                  ' initial byte to be converted to 3 ASCII byte values
    HByte = IByte/100            ' hundreds value as integer
    Tbyte = __Remainder/10       ' divide hundreds remainder by 10; integer value
    SByte = __Remainder          ' remainder after dividing by 10
    
    
    HByte = HByte + 48           ' add decimal value 48 to convert to ASCII value
    TByte = Tbyte + 48           ' add decimal value 48 to convert to ASCII value
    SByte = SByte + 48           ' add decimal value 48 to convert to ASCII value
    
    END
    
    

    Post Edited (Sens-a-Dat) : 3/31/2008 6:39:40 AM GMT
  • John KauffmanJohn Kauffman Posts: 653
    edited 2008-03-31 04:14
    Thanks Gary. Seems elegant. Same number of variables as what I'm using and fewer lines. Might be able to get by with just CurrentByte and RemainderByte. Maybe my loop is over-kill. Thanks
Sign In or Register to comment.