Shop OBEX P1 Docs P2 Docs Learn Events
format conversion... — Parallax Forums

format conversion...

fdelementfdelement Posts: 4
edited 2005-05-18 05:34 in BASIC Stamp
Hi all,

I am currently working on a project which requires the BS2 to read in values (serial) from another device.· I got the BS2 to do so correctly, however I am left with a string in which I need to convert, can anyone help?

CODE
' {$STAMP BS2}
' {$PBASIC 2.5}

Pitch VAR Byte(4)··············· 'Make a 4-byte array
Pitch(3)=0························· 'Put 0 in the last byte
SEROUT 2,16416,[noparse][[/noparse]66]·········· 'Sends command to external device
SERIN 1,16416,[noparse][[/noparse]STR pitch\3]· 'Get 3-byte string
DEBUG "Pitch = "
DEBUG STR pitch,CR··········· ·'Display the string

OUTPUT
Pitch = 7A3

So now I have a string called Pitch that has the values 7A3 (HEX).· I need to convert it to DEC by doing the following...
7*(16^2)+10*(16^1)+3 and save it as a decimal number.· The 10 is A in HEX.· How would I approach this using the BS2 editor?

Any suggestions or comments would be really appreciated!!

Thanks

Comments

  • denodeno Posts: 242
    edited 2005-05-15 02:50
    Use this code:



    DEBUG DEC STR pitch,cr



    deno
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-05-15 15:26
    It might be easiest to let the Stamp SERIN do the conversion, using the HEX3 modifier:

    SERIN 1,16416,[noparse][[/noparse]HEX3 pitch]· 'Get 3-byte hex number

    Don't use BYTE as a variable name--it is a reserved symbol.

    If you need to convert it in place, then something like this...
    X=0
    FOR idx=0 TO 2
    SELECT myByte(idx)
    CASE "0" TO "9"
    X=X*16+(myByte-"0")
    CASE "A" TO "F"
    X=X*16+(myByte-"A"+10)
    CASE ELSE 'huh?
    DEBUG "ERROR IN INPUT",CR
    ENDSELECT
    NEXT

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • fdelementfdelement Posts: 4
    edited 2005-05-15 21:20
    thanks for the replies I will try this out and see how it goes!
  • fdelementfdelement Posts: 4
    edited 2005-05-18 04:53
    i guess i've been asking the wrong questions. Let me reword it again...

    My BS2 will receive a stream of 3 bytes in HEX format. So I thought a good way of reading the three bytes is by using STR. Thus when I run this code I will get the following output...

    CODE
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    Pitch VAR Word················
    SEROUT 2,16416,[noparse][[/noparse]66]··········
    SERIN 1,16416,[noparse][[/noparse]HEX3 pitch]
    DEBUG "Pitch = "
    DEBUG DEC pitch,CR

    OUTPUT
    Pitch = 2048

    I need to some how be able to save this DEC pitch (2048) decimal number to a variable because I will need to perform some mathematical operation on it later.· How can I do that?

    Post Edited (fdelement) : 5/18/2005 5:07:37 AM GMT
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-05-18 05:23
    Well, it seems like what you want is accomplished. The number is now in the variable, pitch. The modifiers like HEX and DEC are simply different ways of expressing the same underlying number. Now you can do things like,
    IF pitch=2048 THEN DEBUG "got it "
    DEBUG HEX pitch, TAB, DEC pitch, TAB, BIN pitch ' prints pitch in different bases

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • fdelementfdelement Posts: 4
    edited 2005-05-18 05:34
    The IF statement you showed will definitely help me out in my code. But is it possible to actually save that 2048 decimal number of 800(HEX) to a variable? Ultimately my code will need to take the difference of two different HEX inputs in DEC format, meaning I will need to subtract 2048 from another decimal value obtained in a similar fashion.


    in any case, Thanks a whole bunch Tracy, you've totally helped me with this issue!
Sign In or Register to comment.