Shop OBEX P1 Docs P2 Docs Learn Events
Var holds a DEC value, need to convert to HEX for output to a device — Parallax Forums

Var holds a DEC value, need to convert to HEX for output to a device

John KauffmanJohn Kauffman Posts: 653
edited 2004-11-29 13:42 in BASIC Stamp
I have a variable holding a decimal value, like 29. I need to send it to a device that only accepts hex, like $1D. Any suggestions for the conversion? Values will only be integers. Using a BS2
·
MyDec VAR byte
MyHex VAR byte
MyDec = 29
… Code needed here to put the value from MyDec into MyHex …
SEROUT· Pin,84,[noparse][[/noparse]…, MyHex, …]
·
In the above pseudo code STAMP would read out $1D for the MyHex argument of the SEROUT
·
The frustrating point is that the DEBUG HEX MyDec does this like a charm - but to the screen.·I need a hex conversion that put result·into a variable.
·
Right now I am embarrassed to say I do it by brute force. I run the MyDec value through a Select Case with three score options to cover my known set of possible values. Each case executes a MyHex = $xxx. There has to be something more clever.
·
Much thanks.

Comments

  • John KauffmanJohn Kauffman Posts: 653
    edited 2004-11-25 14:05
    I got help on this from Solutions Cubed, makers of the device (pocket watch B) and am posting here for future reference of others. The instruction sheet for the device showed data going to the device in hex. However, the device accepts decimal. For example,·sending 11 works just as well as sending $0B.

    I tested & it works well
  • gremlingremlin Posts: 6
    edited 2004-11-28 16:12
    Is is possible to generate your variable in HEX initially and skip the conversion completely?
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-11-29 13:42
    Does the device "want" a single byte value, or two characters that make up the hex byte? If it's a single byte, then no conversion is required, 29 = $1D. If your device wants two bytes, then put the HEX modifier into your SEROUT statement as you have in the DEBUG statement.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
Sign In or Register to comment.