Shop OBEX P1 Docs P2 Docs Learn Events
Simple byte array problem — Parallax Forums

Simple byte array problem

MJHanaganMJHanagan Posts: 189
edited 2011-07-18 11:02 in Propeller 1
Hi everyone - I am having trouble understanding my problem with the attached SPIN code. I initially set a byte array ( Byte TextArray[12] ) to contain the 5-characters "Hello" using a String statement:
TextString := String("Hello")
and it works just fine. Then I attmpt to change the first array element from an "H" to an "h" using the following:
TextString[0] := "h"
which completely screws somethig up since I get garbage when trying to display the contents of TextString on the terminal. I get the same result if I try using the hexidecimal code for "h" as well:
TextArray[0] := $68

Can anyone tell me what I'm doing wrong here? Attached is the SPIN code file I'm using.

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-07-18 08:04
    When you assign a string to a variable, what you're really assigning is the address of the string, not the string itself. To do what you intended, you need to copy the string using bytemove. Be sure to include the zero byte at the end in your byte count.

    -Phil
  • MJHanaganMJHanagan Posts: 189
    edited 2011-07-18 08:42
    When you assign a string to a variable, what you're really assigning is the address of the string, not the string itself. To do what you intended, you need to copy the string using bytemove. Be sure to include the zero byte at the end in your byte count.

    -Phil

    I'm afraid I don't understand why the bytemove command is needed when all I want to do is to change a single byte in the array from "H" to "h". Are you saying I need to do this:
    bytemove( @TextString[ 0 ], $68, 1 )
    instead of:
    TextString[ 0 ] := $68
    ??
  • RaymanRayman Posts: 14,973
    edited 2011-07-18 08:49
    As Phil said, the "String" command returns the address of a string. So, your "TextString" variable contains an address to the data, not the data.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-07-18 09:50
    Here's the code you need:
    bytemove(@TextString, string("Hello"), 6) 'Copy "Hello" to TextString.
    TextString[0] := "h"  'Change the first letter.
    

    -Phil
  • localrogerlocalroger Posts: 3,452
    edited 2011-07-18 09:51
    The string("text") function actually inserts your string inline in the byte code. That's copied into RAM when the Prop boots, but you're actually modifying your program if you change it.

    In your code you're writing the address in your program where the Spin compiler stuck "text",0 into the numeric variable TextString. This needs to be a word or long since it's a pointer to a Hub RAM address. If you were to do something like BYTE[textstring + 2] := "s" you could change the characters of the string image. But if you want to manipulate the string without altering your original code, you need to do something like this:

    var
    byte mystring[8]

    pub
    bytemove(@mystring, string("mytext"),7)
    mystring[2] := "s"
  • RaymanRayman Posts: 14,973
    edited 2011-07-18 10:30
    Another way to go is to put your initial string data into a "DAT" section as a byte array called TextString. Just be sure to terminate your string with a 0 (null character).
    Then, you could alter it the way you initially intended, by doing TextString[ 0 ] := $68
    To use the string you'd usually pass the address of the string, @TextString.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-07-18 11:02
    Here's an example of what Rayman suggests (from a program I'm presently working on).
    PUB   
      packetTemplate[9] := "F"
      packetTemplate[10] := "F"
    ' now packetTemplate  array equals "what is $FF + $00?", 0
     
    DAT  ' I use the numbers to help keep track of which elements to change.
                                           '1         2         3
                                 '01234567890123456789012345678901
    packetTemplate          byte "what is $00 + $00?", 0
     
    

    Duane
Sign In or Register to comment.