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

Number to String

CumQuaTCumQuaT Posts: 156
edited 2010-06-15 13:57 in Propeller 1
Hi all,

I was wondering if anyone knew of a way to convert a number (such as a byte or long value) into a string. Normally in another programming language like BASIC you would just use STR$(Value) and bam, job's done, but in Spin, I can't seem to get it to work...

I'm building an RFID storage system and I need to store the RFID codes I scan into a text file on a flashcard and no matter what I do I can't get it to save the numbers into the text file...

Any ideas?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Who are you, and why are you reading my signature?

Comments

  • sylvie369sylvie369 Posts: 1,622
    edited 2010-06-15 01:51
    Have you looked at the Numbers object in the OBEX? It contains the following method:

    PUB ToStr(Num, Format): StrAddr
    {{Convert long Num to z-string using Format; returns string address.
      PARAMETERS: Num     = 32-bit signed value to translate to ASCII string.
                  Format  = Indicates output format: base, width, grouping, etc. See "FORMAT SYNTAX" for more information.
      RETURNS:    Actual length of output string, not including null terminator.}}
      BCXToText(Format >> 19 & 7, Format >> 13 & $3F, Format >> 12 & 1, Format >> 11 & 1, Format >> 5 & $3F, BinToBCX(Num, Format & $1F #> 2 <# 16))
      StrAddr := @StrBuf
    
  • JasonDorieJasonDorie Posts: 1,930
    edited 2010-06-15 01:56
    Check out the FullDuplexSerial or SimpleSerial objects - Both have Dec() functions that take a numeric value and convert it to a sequence of digit characters to send to the serial port.

    The simplest version, in pseudo code:

    
    byte buffer[noparse][[/noparse] 10 ]
    
    // Build a buffer of number characters, from the one's place to the higher ones...
    int index = 0
    do {
      buffer[noparse][[/noparse] index ] = '0' + (value % 10)
      index += 1
      value /= 10
    } while( value > 0 )
    
    // now print the digits from index-1 down to 0  (the number is reversed to simplify the implementation)
    
    



    Added: or yeah, what Sylvie said. [noparse]:)[/noparse]
  • max72max72 Posts: 1,155
    edited 2010-06-15 07:47
    You'll find many different "numbers" objects.
    One of my preferred for simple tasks is simple_numbers
    obex.parallax.com/objects/182/

    Numbers is the bigger brother, but sometimes is overkill. Formatting is more powerful, but more complex.
    obex.parallax.com/objects/23/

    Massimo
  • CumQuaTCumQuaT Posts: 156
    edited 2010-06-15 13:57
    Fantastic! Thanks for the help! Good ol' oBex!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Who are you, and why are you reading my signature?
Sign In or Register to comment.