PDA

View Full Version : A question on strings.....



Circuitbuilder9
04-07-2012, 10:13 PM
Hi, all.

Two questions:

if a variable is an array, and you "bytemove" like this:

byte array[30]
byte copy[90]

bytemove(@array, @copy, 30),

will the entire string of bytes be copied to the entire array of (array), since there are only 30 of the bytes, and only 30 bytes are being copied from a larger string?

Number 2,

In a str(string) instruction, if you copied characters by each byte onto the string, how would you code that?

like this? : (ex) lcd.str(@array)

Would the entire string put on previously (if that's the answer) into the array of (array)?

We'll use the string from the previous question.

Mike Green
04-07-2012, 10:43 PM
1) The BYTEMOVE you wrote will copy exactly 30 bytes, no more, no less. It doesn't matter how the bytes are organized in the array copy. Remember that strings don't really exist in Spin other than as an abstraction imposed on arrays of bytes.

2) There isn't really a "str(string)" instruction. There happen to be subroutines provided as part of several I/O objects (packages) that take the starting address of an array of bytes that contains a 'string' of characters terminated by a byte containing zero. This is considered to be a 'string' by convention.

A ".str" method in an I/O object, say for displaying on an LCD, is passed the starting address of a byte array, picks up bytes from the array, one at a time, and, if the byte isn't a zero, passes the byte to a single character display routine for the LCD. If the byte is zero, the routine exits. It looks like this:
PUB str(address) : c
' Fetch the byte value and leave in "c". Increment the address and test for a zero value
repeat while c := byte[address++]
out(c) ' Pass the byte to the output routine while it's non-zero

Circuitbuilder9
04-07-2012, 11:24 PM
Thanks!!!

One more thing: would this piece of code work?


bytemove(@sbuf, @mbuf, 32)
lcd.str(@sbuf)

I am using Serial_Lcd as the lcd.object.
The mbuf is the main piece of larger arrays, and the sbuf is the array where it only holds 32 bytes.
I want to know if the 32 bytes (which will be characters) will be interfaced correctly.

Ariba
04-08-2012, 12:39 AM
If it works depends on the content of mbuf. If mbuf contains a string shorter than 32 bytes it will work.
It's better to copy only 31 bytes, so you have always an ending zero (sbuf is initailized with all zeroes on declaration).

bytemove(@sbuf, @mbuf, 31)
lcd.str(@sbuf)

Andy

Mike Green
04-08-2012, 12:42 AM
Remember that the string in sbuf has to have a zero byte at the end. The string contained in sbuf (or mbuf) can contain at most 31 characters (to allow for this extra byte).

Circuitbuilder9
04-08-2012, 01:31 AM
So, does that mean i have to initialize variables one short because of the zero as the one?

turbosupra
04-08-2012, 05:05 AM
Here is how I use bytemove

bytemove(b_Ptr2b_RxString, RxStringAddr, strsize(RxStringAddr))
and
bytemove(@b_suffix, RxStringAddr, strsize(RxStringAddr) +1) ' copy from RxStringAddr to @suffix, size + 1 to have the terminating 0

Or in your case

bytemove(@array, @copy, strsize(@copy))

and for bytefill

bytefill(@tmp_addr, 0, strsize(tmp_addr))




Hi, all.

Two questions:

if a variable is an array, and you "bytemove" like this:

byte array[30]
byte copy[90]

bytemove(@array, @copy, 30),

will the entire string of bytes be copied to the entire array of (array), since there are only 30 of the bytes, and only 30 bytes are being copied from a larger string?

Number 2,

In a str(string) instruction, if you copied characters by each byte onto the string, how would you code that?

like this? : (ex) lcd.str(@array)

Would the entire string put on previously (if that's the answer) into the array of (array)?

We'll use the string from the previous question.


So, does that mean i have to initialize variables one short because of the zero as the one?