Byte array to String
stamped
Posts: 68
I am writing a very simple string library that performs some basic functions. I have got concatenation working through byte arrays, but I am wanting to convert the byte array back to a string. I am not having any luck with bytemove.
·
Another couple of questions:
- I·am also unsure how to determine the length of an array, such as the number of characters in a byte array?
- I would also like to be able to set the size of an array after the fact, ie create one in the body of the method rather than pre-define in the method header.
Post Edited (stamped) : 7/17/2007 2:52:33 PM GMT
·
PUB tester | temp1, temp2, length1, length2, val[noparse][[/noparse]7], count, copystr temp1 := string("test") temp2 := string("ing") ' CONVERTING FROM A STRING TO A BYTE ARRAY (pre-defined hard coded size -> painful) length1 := strsize(temp1) ' 4 count := 0 repeat length1 val[noparse][[/noparse]count++] := byte[noparse][[/noparse]temp1++] 'All copied into val length2 := strsize(temp2) repeat length2 val[noparse][[/noparse]count++] := byte[noparse][[/noparse]temp2++] ' OUTPUTTING THE CONCATENATED STRINGS count := 0 repeat 7 Serial.tx(val[noparse][[/noparse]count++]) Serial.str(string(" ")) [b]' NOTE THIS IS CONVERTING BACK TO A STRING [/b] [b]bytemove(@copystr, @val, strsize(@val)+1) [/b]' Even tried hard coding 7,6,5,4.. Serial.str(copystr) ' Outputs garbage, have also tried passing the address (@copystr) without luck
Another couple of questions:
- I·am also unsure how to determine the length of an array, such as the number of characters in a byte array?
- I would also like to be able to set the size of an array after the fact, ie create one in the body of the method rather than pre-define in the method header.
Post Edited (stamped) : 7/17/2007 2:52:33 PM GMT
Comments
You can't determine the size of a declared array. It's not recorded anywhere. People often define a named constant with the string array size(s) they plan to use, declare the byte arrays using the name, and have that name to use in bytemoves and similar such things. That's the best you can do.
If you're going to make up an object that handles strings as byte arrays, you can establish any conventions you want. The two most common are: 1) Using a zero byte as a string terminator (the "C" convention); 2) Using a length byte as the first byte of the array with the characters following the length (the "Pascal" convention). Neither provide for an indication of the allocated string length. This can be done with a byte preceeding the string area in memory so we have: <size of array> <length of string> <string> or <size of array> <string> <terminator>.
If you want to allocate space in your code (dynamically) you're describing a storage allocator. There's none built into Spin. Someone did develop a string manipulation object (check the Object Exchange and the forum) which has a simple storage allocator as part of it. You still have to declare the actual storage pool somewhere. There are all sorts of issues that come up around what to do to reuse the space for strings that aren't used anymore and how to figure that out. Have a look at the Wikipedia under "dynamic memory allocation" for a discussion.
Post Edited (Mike Green) : 7/17/2007 3:09:39 PM GMT
With regards to the declaration of var as a long, how do I define the methods variable "var" as a Byte array (can I cast it)? I am new to Spin, so I am slowly coming to grips with the semantics of the language.
As far as dynamic storage allocation is concerned, if I cannot create a variable on the fly I think this would be a bit beyond my abilities at this stage. Unless I could declare a global variable and use that as the size of the method variable (I assume this will not work):
PUB tester new_array | val[noparse][[/noparse]global_var_size]
return val
Again, Spin does not do dynamic memory allocation. All arrays have constant bounds. This lets the compiler always know the address of the variable (for local variables, the address will change, but the relative stack offset is fixed). Since Spin supports pointers, you are free to do your own dynamic memory allocation, but there's no other support for it.
If you could point me to some documentation that thoroughly explains the typing I would appreciate it. At the moment, it is one step forward, and two backwards.
Post Edited (stamped) : 7/18/2007 11:14:25 AM GMT
For anyone else reading this, I think that the reason it works, is because the "byte[noparse][[/noparse]@val]"· passes the location in memory·of the data in the "val" array, and the count++ sets the offset (starting position)·to the data that sits in val.
Another question Mike that I have come across is why does the following not work (prints garbage):
I hope my 20 questions are helping some other newbies understand Spin [noparse]:)[/noparse]
byte[noparse][[/noparse]@val] works because it is a byte array that begins at the same memory location as the variable "val" regardless of how "val" is declared. It's a combination of pointer dereferencing and casting if you want to use C terminology.
Post Edited (Mike Green) : 7/18/2007 1:00:55 PM GMT
It was started a few days ago by HYDRA enthusiasts, and primarily is focused on video drivers and games, but there is no reason why it cant be used for more generic propeller issues too.
It can be found here propeller.wikispaces.com/.
Ill make a more formal announcement on a separate message too, so it's more visible for casual readers of this forum.
Mahjongg