Shop OBEX P1 Docs P2 Docs Learn Events
How do I convert the hex value from a buffer to display in decimal? — Parallax Forums

How do I convert the hex value from a buffer to display in decimal?

Don MDon M Posts: 1,653
edited 2012-06-07 05:53 in Propeller 1
I am receiving 2 bytes of data into a buffer-
             cdinx := 2
             chksum := $03
             repeat
                s := receive.rx_time(10)
                if (s & $1_00)
                   chksum &= $0_FF
                    p := 1 
                   pollack
                   quit
                else
                   p := 0   
                chksum += s
                buffer1[cdinx] := s
                cdinx += 1      

And then displaying it using this-
   vmc.CdBeginSesX(@buffer1)

   term.str(string("Credit: "))
   term.hex(buffer3[2], 2)
   term.hex(buffer3[3], 2)

They are stored as hex values and will print that way but I need to see them in decimal form. For example let's say I have 01 2C hex stored in the buffer which is the equivalent of 300 decimal. Using term.hex I get 012C displayed on the terminal.

But if I use term.dec I get 144 which is the 01 converted to 1 and the 2C converted to 44. How do I put the 012C together so it converts to 300?

Thanks.
Don

Edit: changed to solved.

Comments

  • jmgjmg Posts: 15,183
    edited 2012-06-06 13:11
    Don M wrote: »
    How do I put the 012C together so it converts to 300?

    My calculator says this
    0x1*256+0x2C = 300
    ie simply multiply the high byte by 256 and add to the lower byte.
  • Don MDon M Posts: 1,653
    edited 2012-06-06 14:00
    Maybe the question should be- how do I retrieve the 2 bytes together as one word (?) or long (?) and then just use term.dec to display?
  • Don MDon M Posts: 1,653
    edited 2012-06-06 14:13
    Ok I got it to work like this-
    term.str(string("Credit: "))
       credit := (buffer1[2]) * 256
       credit += (buffer1[3])
       term.dec(credit)
    

    Is this the proper way? I defined credit as a long.

    Thanks.
    Don
  • Mike GMike G Posts: 2,702
    edited 2012-06-06 19:31
    Don M, here's another way to look at it. This example places the value 1024 ($0400) into memory one byte at a time - $00 followed by $04. The word command is used to return a word aligned data type from memory. Check out byte, word, and long commands in the Propeller manual.
    CON
      _clkmode = xtal1 + pll16x     
      _xinfreq = 5_000_000
    
    DAT
      buff  byte  $2[0]
    
    VAR
    
    OBJ
      pst           : "Parallax Serial Terminal"
    
    PUB Main
    
      pst.Start(115_200)
      pause(500)
    
      buff[0] := $00
      buff[1] := $04
    
      pst.dec(word[@buff])
      
      
    PRI pause(Duration)  
      waitcnt(((clkfreq / 1_000 * Duration - 3932) #> 381) + cnt)
      return
    
  • Mike GreenMike Green Posts: 23,101
    edited 2012-06-06 19:48
    Note that the above won't work if buff is not word aligned. Much better would be to declare a word or long value and then access the individual bytes to set it up like:
    PUB example | temp
       temp.byte[0] := $00   ' least significant byte
       temp.byte[1] := $04   ' most significant byte
       pst.dec(temp.word)   ' display word value in decimal
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-06-06 20:32
    Not that this is the best way to do this, but if you do want a byte buffer long aligned you can use the org statement.
    DAT
               org
      buff    byte  0[2]
    

    BTW, Mike G, you got your two and zero switched around. I assume you want two bytes of zeros and zero bytes of twos.
  • AribaAriba Posts: 2,690
    edited 2012-06-06 21:29
    Don M wrote: »
    Ok I got it to work like this-
    term.str(string("Credit: "))
       credit := (buffer1[2]) * 256
       credit += (buffer1[3])
       term.dec(credit)
    

    Is this the proper way? I defined credit as a long.

    Thanks.
    Don

    You don't need the parentheses and you can write it on one line:
    credit := buffer1[2] * 256 + buffer1[3]
    

    Because a byte has 8 bits you can also shift the high byte by 8 (it's the same as * 256, but faster):
    credit := buffer1[2] << 8 + buffer1[3]
    

    Andy
  • Don MDon M Posts: 1,653
    edited 2012-06-07 05:53
    Ariba wrote: »
    You don't need the parentheses and you can write it on one line:
    credit := buffer1[2] * 256 + buffer1[3]
    

    Because a byte has 8 bits you can also shift the high byte by 8 (it's the same as * 256, but faster):
    credit := buffer1[2] << 8 + buffer1[3]
    

    Andy

    Andy- Thanks for those tips!
Sign In or Register to comment.