Shop OBEX P1 Docs P2 Docs Learn Events
Converting bytes to Strings — Parallax Forums

Converting bytes to Strings

dermotdermot Posts: 26
edited 2011-11-03 12:35 in Propeller 1
Hi,

Does anyone know how to convert bytes/words/longs to strings and strings to bytes/words/longs. Is there an object pre-written. I cant find any.

Comments

  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-11-03 09:10
    Not quite sure what you mean, but strings are bytes. In SPIN, a string is an array of bytes that is zero terminated.

    If you want to make a string of bytes you could (in a DAT section), do this:
    byte string1 "I"," ","a","m"," ","h","e","r","e",0 'notice each letter is handled as a byte
    or more simply:
    byte string1 "I am here",0
    these two are handled the same way. Both can be handled as bytes or as a string.

    I have an object (in my signature) that does string manipulation, if that is what you are looking for.

    EDIT: Possibly you are looking to convert numeric values to strings? Then look for the "Simple_Numbers" object in the OBEX (this one, I think...it has a different name), but you already have it, because it came with the Propeller Tool.
  • dermotdermot Posts: 26
    edited 2011-11-03 09:39
    Thanks Bobb,

    Sorry for confusion I was indeed trying to convert numeric values to strings
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-11-03 11:24
    Look at the "dec" method in FullDuplexSerial.

    It shows how to convert a value to ASCII characters.

    Duane
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-11-03 11:26
    Several objects contain conversion functions. For example the FullDuplexSerial, I think TV Text/VGA drivers also allows to print numbers. Some LCD drivers can do the conversion. These usually are called .hex, .bin and .dec and convert from number to string.

    I think there is also a string object somewhere which can also convert from string to number.

    The ConfigReader object in the tools section also converts from string to number.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-11-03 12:06
    I'd like to add one more method to MagIO2's list.

    The Parallax Serial Terminal (PST) object has a very nice little method called StrToBase. Here's the method:
    PRI StrToBase(stringptr, base) : value | chr, index
    {Converts a zero terminated string representation of a number to a value in the designated base.
    Ignores all non-digit characters (except negative (-) when base is decimal (10)).}
      value := index := 0
      repeat until ((chr := byte[stringptr][index++]) == 0)
        chr := -15 + --chr & %11011111 + 39*(chr > 56)                              'Make "0"-"9","A"-"F","a"-"f" be 0 - 15, others out of range     
        if (chr > -1) and (chr < base)                                              'Accumulate valid values into result; ignore others
          value := value * base + chr                                                  
      if (base == 10) and (byte[stringptr] == "-")                                  'If decimal, address negative sign; ignore otherwise
        value := - value
           
    
    

    To bad it's a private method. I think it would be more useful if it had been a public method.

    I just recently discovered this useful method; I thought I'd let others know about it.

    Duane
  • tonyp12tonyp12 Posts: 1,951
    edited 2011-11-03 12:35
    dermot wrote: »
    Sorry for confusion I was indeed trying to convert numeric values to strings

    Really simple if you are Ok with a HEX representation of the value.
    each 4 bits will be the 0 to F, so just add 48 to it and if that result is higher than 57 add another 8 to it.

    Dec is a little harder, but if you know your value will always be between 0-99 (two digits) then some shortcuts could be implemented.
Sign In or Register to comment.