Shop OBEX P1 Docs P2 Docs Learn Events
How do you assign a string ? — Parallax Forums

How do you assign a string ?

BeanBean Posts: 8,129
edited 2007-06-07 23:31 in Propeller 1
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
·

Comments

  • CJCJ Posts: 470
    edited 2007-03-27 12:28
    use a bytemove(@tempstr, @string, strsize(@string))

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Parallax Forums - If you're ready to learn, we're ready to help.

    Post Edited (CJ) : 3/27/2007 12:33:42 PM GMT
  • BeanBean Posts: 8,129
    edited 2007-03-27 13:02
    Thanks, that makes alot more sense.

    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
    ·
  • KaioKaio Posts: 253
    edited 2007-03-27 13:34
    A good way for this is to locate your strings in the DAT section.
    VAR
      BYTE tempStr[noparse][[/noparse]255]
    
    PUB fillStr | size
    
      size := strsize(@myText) 
      bytemove(@tempstr, @myText, size)
      tempStr[noparse][[/noparse]size] := 0
      
    
    DAT
    myText                  byte    "My Text",0
    
    
  • CJCJ Posts: 470
    edited 2007-03-27 16:11
    I forgot to move the 0 terminator, so it would be

    bytemove(@tempstr, @string, strsize(@string) + 1)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Parallax Forums - If you're ready to learn, we're ready to help.
  • Chicago MikeChicago Mike Posts: 88
    edited 2007-06-06 23:33
    Relating to this: Is it possible to reference a single character in the string array and move it to a new variable as well using this method? Just use a String size of 1? I've been having an issue extracting a single character from an input serial string. Just curious

    temp:= strarray ' doesn't work to get the 4th character.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-06-06 23:47
    In that case you can use the BYTE operator:

    temp := BYTE[noparse][[/noparse]@strarray][noparse][[/noparse]x]
    

    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.
  • Chicago MikeChicago Mike Posts: 88
    edited 2007-06-07 12:00
    HUmmm Thats what I've been trying with no luck. Below is the code I'm running. I've declared in the VAR section: byte buf[noparse][[/noparse]127]
    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
  • CardboardGuruCardboardGuru Posts: 443
    edited 2007-06-07 15:07
    Hi Mike,

    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 BakerPaul Baker Posts: 6,351
    edited 2007-06-07 15:51
    He is correct, tv.str expects a pointer to a string. A string is a series of characters followed by a 0, so the following code would work

    temp.byte[noparse][[/noparse]0] := BYTE[noparse][[/noparse]@buf]
    temp.byte[noparse][[/noparse]1] := 0
    tv.string(@temp) 
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • Chicago MikeChicago Mike Posts: 88
    edited 2007-06-07 23:12
    I see the error of my ways, and that makes sense. I tried it and I do get a copy, zero terminated into temp and can display it. My issue is that I can't figure out how to pluck a single character. Say I want to only copy the 3rd character of the string in buf into the temp.byte[noparse][[/noparse]0]. Thats were I'm having difficulties. Since I'm referencing buf at the address, do I have to somehow offset the address of buf?

    So from BOB as the string, I want to only grab the second B and save it in temp.

    Thanks!
  • CardboardGuruCardboardGuru Posts: 443
    edited 2007-06-07 23:16
    This should do it.

    temp.byte[noparse][[/noparse] 0] := BYTE[noparse][[/noparse]@buf][noparse][[/noparse] 2]
    temp.byte[noparse][[/noparse] 1] := 0
    tv.string(@temp)
    

    Post Edited (CardboardGuru) : 6/7/2007 11:21:07 PM GMT
  • Chicago MikeChicago Mike Posts: 88
    edited 2007-06-07 23:30
    I gave that a shot as I thought that syntax would work as well, however I don't get anything.

    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.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-06-07 23:31
    Chicago Mike said...
    I gave that a shot as I thought that syntax would work as well, however I don't get anything.

    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
    This code should not display BOB, it should display B

    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
Sign In or Register to comment.