How do you assign a string ?
Bean
Posts: 8,129
In my overlay code I have a variable "BYTE tempStr[noparse][[/noparse]255]". Now I want to assign a string to that variable, but I cannot figure out how. I tried tempStr:=STRING("text"), but STRING returns the address of the text.
So I ended up doing this:
······· tempStr[noparse][[/noparse]0]:="-"
······· tempStr[noparse][[/noparse]1]:="-"
······· tempStr[noparse][[/noparse]2]:=":"
······· tempStr[noparse][[/noparse]3]:="-"
······· tempStr[noparse][[/noparse]4]:="-"
······· tempStr[noparse][[/noparse]5]:=" "
······· tempStr[noparse][[/noparse]6]:="-"
······· tempStr[noparse][[/noparse]7]:="-"
······· tempStr[noparse][[/noparse]8]:="/"
······· tempStr[noparse][[/noparse]9]:="-"
······· tempStr[noparse][[/noparse]10]:="-"
······· tempStr[noparse][[/noparse]11]:="/"
······· tempStr[noparse][[/noparse]12]:="-"
······· tempStr[noparse][[/noparse]13]:="-"
······· tempStr[noparse][[/noparse]14]:=0
Surely there is a better way ?
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"Educate your children to self-control, to the habit of holding passion and prejudice and evil tendencies subject to an upright and reasoning will, and you have done much to abolish misery from their future and crimes from society"
Benjamin Franklin
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
So I ended up doing this:
······· tempStr[noparse][[/noparse]0]:="-"
······· tempStr[noparse][[/noparse]1]:="-"
······· tempStr[noparse][[/noparse]2]:=":"
······· tempStr[noparse][[/noparse]3]:="-"
······· tempStr[noparse][[/noparse]4]:="-"
······· tempStr[noparse][[/noparse]5]:=" "
······· tempStr[noparse][[/noparse]6]:="-"
······· tempStr[noparse][[/noparse]7]:="-"
······· tempStr[noparse][[/noparse]8]:="/"
······· tempStr[noparse][[/noparse]9]:="-"
······· tempStr[noparse][[/noparse]10]:="-"
······· tempStr[noparse][[/noparse]11]:="/"
······· tempStr[noparse][[/noparse]12]:="-"
······· tempStr[noparse][[/noparse]13]:="-"
······· tempStr[noparse][[/noparse]14]:=0
Surely there is a better way ?
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"Educate your children to self-control, to the habit of holding passion and prejudice and evil tendencies subject to an upright and reasoning will, and you have done much to abolish misery from their future and crimes from society"
Benjamin Franklin
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Parallax Forums - If you're ready to learn, we're ready to help.
Post Edited (CJ) : 3/27/2007 12:33:42 PM GMT
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"Educate your children to self-control, to the habit of holding passion and prejudice and evil tendencies subject to an upright and reasoning will, and you have done much to abolish misery from their future and crimes from society"
Benjamin Franklin
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
bytemove(@tempstr, @string, strsize(@string) + 1)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Parallax Forums - If you're ready to learn, we're ready to help.
temp:= strarray ' doesn't work to get the 4th character.
where x is the zero based character position you want to copy (x=0 is the first character)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.
I"m using this with a serial receiver, and the string its receiving right now as a test is 'BOB'. The string is being received, and I can display the entire string, but the plucking of a single character is the issue I'm having
PUB radiorx | temp
repeat
ifnot ina[noparse][[/noparse]Busy]
ina[noparse][[/noparse]Ready]:=Low
buf:=serial.rx
ina[noparse][[/noparse]Ready]:=High
temp:=BYTE[noparse][[/noparse]@buf]
tv.str(@buf) '' Displays the entire string without a problem
tv.str(temp) " Displays nothing
else
waitcnt(500_000+cnt)
I've got to be missing something stupid
In the first case you're passing a pointer to a string terminated by zero.
In the second case you're passing a long with the actual character in the low 8 bits.
If you passed tv.str(@temp) I would think it would (accidentally) work, because the long is stored in little endian format, and the upper 24 bits are zero, which will provide the zero termination on the string.
i.e.
if the string is BOB,
temp:=BYTE[noparse][[/noparse]@buf] will cause temp to be equal to $00000042. And this is stored in memory the same way as BYTE $42,$00,$00,$00.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.
So from BOB as the string, I want to only grab the second B and save it in temp.
Thanks!
Post Edited (CardboardGuru) : 6/7/2007 11:21:07 PM GMT
temp.byte[noparse][[/noparse] 0] := BYTE[noparse][[/noparse]@buf]
temp.byte[noparse][[/noparse] 1] := 0
tv.string(@temp)
So the above proves that the variable is copying to temp, as that the tv.string displays BOB . However when I add the reference to any array record of buf (Single character), I get nothing. (Below like you said). IS there some problem with referencing an index off of a referenced address like this?
temp.byte[noparse][[/noparse] 0] := BYTE[noparse][[/noparse]@buf][noparse][[/noparse] 2]
temp.byte[noparse][[/noparse] 1] := 0
tv.string(@temp)
Very odd.
Take a look at page 167 and 168 of the manual, double indexing is a valid syntax and it works.
Get back to basics, instead of trying to cast the character into a string use tv.out (or tv.print depending on which driver you are using) so do tv.out(BYTE[noparse][[/noparse]@buf][noparse][[/noparse] 2])
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.
Post Edited (Paul Baker (Parallax)) : 6/7/2007 11:39:32 PM GMT