View Full Version : Converting bytes to Strings

11-03-2011, 03:39 PM

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.

Bobb Fwed
11-03-2011, 04:10 PM
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 (http://obex.parallax.com/objects/23/), I think...it has a different name), but you already have it, because it came with the Propeller Tool.

11-03-2011, 04:39 PM
Thanks Bobb,

Sorry for confusion I was indeed trying to convert numeric values to strings

Duane Degn
11-03-2011, 06:24 PM
Look at the "dec" method in FullDuplexSerial.

It shows how to convert a value to ASCII characters.


11-03-2011, 06:26 PM
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 Degn
11-03-2011, 07:06 PM
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.


11-03-2011, 07:35 PM
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.