Simple question: How to manipulate the bytes of a string
ElectricAye
Posts: 4,561
I'm unfamiliar with how strings are manipulated so I'm hoping to get some wisdom regarding the following:
I've loaded a string into a variable like so:
And when I do the following, everything displays on the VGA very nicely:
So far, so good.
But now I would like to extract and display only the 9th byte of Header, which should be a "T", I think.
For extraction and later use in making comparisons, my guess would be to use something like this to get to the 9th byte:
Byte[(HeaderByteAddressPointer+8)]
And for only displaying the 9th byte of Header, I would guess something like this would work:
VGA.str((HeaderByteAddressPointer+8))
But so far I haven't gotten any of these things to work out. I'm guessing it has something to do with strings needing a zero terminator, but I'm not sure how that is done, or if there is some simpler approach, or somebody has an Object for dealing with this kind of string thing.
Thanks!
I've loaded a string into a variable like so:
{When using bytemove to move a string, be sure to include a space for the zero terminator... } Bytemove(@Header, string("650,400,T,500"), 14)
And when I do the following, everything displays on the VGA very nicely:
{Assign the memory address of Header to the HeaderByteAddressPointer...} HeaderByteAddressPointer := @Header {Display the entire header...} VGA.str(HeaderByteAddressPointer)
So far, so good.
But now I would like to extract and display only the 9th byte of Header, which should be a "T", I think.
For extraction and later use in making comparisons, my guess would be to use something like this to get to the 9th byte:
Byte[(HeaderByteAddressPointer+8)]
And for only displaying the 9th byte of Header, I would guess something like this would work:
VGA.str((HeaderByteAddressPointer+8))
But so far I haven't gotten any of these things to work out. I'm guessing it has something to do with strings needing a zero terminator, but I'm not sure how that is done, or if there is some simpler approach, or somebody has an Object for dealing with this kind of string thing.
Thanks!
Comments
Probably v1 would be easier to work with.
You have the idea correct, but I think you need to copy the byte you want (Byte[(HeaderByteAddressPointer+8)]) to a new array, and terminate it.
Bobb,
those objects look very handy. I'll try them out and see how they work with this.
It seems tragic that SPIN doesn't have a simple built-in way of dealing with string operations of this sort, especially considering it has such a dandy video output.
Thanks for such a fast response!
Mark
If your 32kB code should happen to have no zero anywere 32kB of characters would be outputted.
So zero-termination is a must.
If you want to extract parts of a string you would have to call methods that only send a single byte
or like Bobb showed copying the bytesequence to an temprorary array one byte bigger than your extracted string
where this additional byte contains a zero.
So far, so good.
But now I would like to compare that first character (i.e, the "6") extracted from the Header with some other string characters and this does not seem to work:
I'm guessing my problem here is that "2" and "9" are not the proper way to express this.
Another guess: FwedStrings.SubStr(@Header, 0, 1) results in an address of the string, not the string itself, so how can that address get converted to a string???
Any suggestions?
You would need to use something like this (below) to convert the strings to decimals, then compare those to the desired numbers.
That might work as you expect it to (only works with single digit numbers).
Okay, I see. Man, this string wrangling business has been tying me in knots!
Thanks for the help! :-)
And lack of typecasting or size checks makes debugging a headache too, if you make a typo.