Shop OBEX P1 Docs P2 Docs Learn Events
Decimal to String — Parallax Forums

Decimal to String

crgwbrcrgwbr Posts: 614
edited 2006-10-10 21:31 in General Discussion
Hello,
Is it possible to convert a decimal (like 2348) to a string ("2348")·without using an IF THEN ELSEIF command.· I have numbers (coming from an encoder) that I need to display on an LCD,· LCD's only display strings.· Thanks.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
NerdMaster
For
Life

Comments

  • YendorYendor Posts: 288
    edited 2006-10-10 01:50
    You can add 30 to the value to convert to ascii

    example:
    30 + 0 = 30 = ASCII '0'
    30 + 1 = 31 = ASCII '1'
    etc.

    Rodney
  • Mike CookMike Cook Posts: 829
    edited 2006-10-10 02:04

    Just to clarify Rodney's answer that would be hex 30 or decimal 48.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike
  • YendorYendor Posts: 288
    edited 2006-10-10 03:53
    Thanks Mike!

    BTW, I'm starting to give my age in hex as people don't seem to question the $ [noparse]:o[/noparse])

    Rodney
  • BeanBean Posts: 8,129
    edited 2006-10-10 11:49
    Great idea Rodney !
    Heck I'm only $27 years old...

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com

    Don't mistake experience for intelligence. And vis-vera.
    ·
  • crgwbrcrgwbr Posts: 614
    edited 2006-10-10 12:10
    Thanks Rodney,

    That would help a lot, only problem is I have a range of·decimals -2500 through 2500 that I need to convert to the·same number, only in string format instead of decimal.· I'm probable going to switch this project to·a BS2.· I'm more familliar with PBASIC that SX/B.· But if anyone knows how to do this in SX/B, please speak up.

    Thanks a lot.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    NerdMaster
    For
    Life
  • Mike CookMike Cook Posts: 829
    edited 2006-10-10 12:17
    You'll need to write a subroutine to do that, In fact I thought someone posted one a while back that would do a 4 or 5 digit word value, you might want to dig through the SX forum.·Anyway here is one to do a 3 digit decimal number:

    ' -----------------------------------------------------------------------------
    ' Use: TX_DEC3 value aByte
    ' -- displays value in Decimal format (text), Does not supress leading Zero's
    ' --
    
    TX_DEC3:
      
      temp2 = __PARAM1                      ' copy output value
      temp2 = temp2 / 100                   ' get 100's digit
      temp3 = __REMAINDER                   ' save 10's & 1's
      temp1 = "0" + temp2                   ' make 100's character
      TX_BYTE temp1                         ' print it
      temp2 = temp3 / 10                    ' get 10's digit 
      temp3 = __REMAINDER                   ' save 1's
      temp1 = "0" + temp2                   ' make 10's character
      TX_BYTE temp1                         ' print it
      temp1 = "0" + temp3                   ' make 1's character
      TX_BYTE temp1                         ' print it
      RETURN                                ' return to caller
     
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike

    Post Edited (Mike Cook) : 10/10/2006 12:26:39 PM GMT
  • crgwbrcrgwbr Posts: 614
    edited 2006-10-10 16:46
    Thanks Mike,
    As soon as I get my serial cable back from my dad, I'll try that. It doesn't look like it will be to difficult to modify for 4 digits. Thanks Again.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    NerdMaster
    For
    Life
  • James NewtonJames Newton Posts: 329
    edited 2006-10-10 18:45
    http://www.sxlist.com/techref/ubicom/lib/math/radix/index_sx.htm ·has a page with many subroutines that can do that. Your value is going to need to be converted to a positive binary number first and then to an ASCII string. So if it is negative, print the "-" then make it positive (number = 0-number) and then in either case, call one of the subroutines for 12 binary bits or better (2^12=4096 > 2500 > 2^11=2048) to make an ASCII string.

    Probably the best fit is this one:

    http://www.sxlist.com/techref/ubicom/lib/math/radix/b2a-16b5a-rl_sx.htm which is 16 bits in and 5 digits out.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---
    James Newton, Host of SXList.com
    james at sxlist,com 1-619-652-0593 fax:1-208-279-8767
    SX FAQ / Code / Tutorials / Documentation:
    http://www.sxlist.com Pick faster!



    Post Edited (James Newton) : 10/10/2006 6:49:08 PM GMT
  • crgwbrcrgwbr Posts: 614
    edited 2006-10-10 20:30
    Mike, just double checking, to call the TX_DEC3 subroutine, you would use something like

    TX_DEC3 150

    correct?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    NerdMaster
    For
    Life
  • Mike CookMike Cook Posts: 829
    edited 2006-10-10 21:31
    The way I use this routine would be to pass the number via a variable:

    idx = 150

    TX_DEC3 idx



    Also to use the SUB you will need to define it, take a look at the attached file.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike
Sign In or Register to comment.