ROBOTC.net forums
http://www.robotc.net/forums/

Converting a string into a byte array
http://www.robotc.net/forums/viewtopic.php?f=1&t=4376
Page 1 of 2

Author:  mattallen37 [ Wed Mar 21, 2012 5:01 pm ]
Post subject:  Converting a string into a byte array

How should I convert a string into a byte array in ROBOTC?

For example, if I have the string "Hi" I want a byte array of 2 elements, with the values of 0x48 and 0x69 (or three elements, including the NULL terminator, it doesn't really matter).

Author:  mightor [ Wed Mar 21, 2012 5:49 pm ]
Post subject:  Re: Converting a string into a byte array

I would usually just memcpy() it :)

Edit:
Something along the lines of:
Code:
string foo = "hi";
byte baz[2];
memcpy(baz, foo, strlen(foo));

- Xander

Author:  mattallen37 [ Wed Mar 21, 2012 8:53 pm ]
Post subject:  Re: Converting a string into a byte array

Thanks.

Now, how can I set the size of the array (number of elements) to the length of the string? Or does memcpy do that for me?

Author:  NeXT-Generation [ Wed Mar 21, 2012 9:07 pm ]
Post subject:  Re: Converting a string into a byte array

I believe that is the function of strlen(foo). At least, I think that's it. Mightor?

Author:  mattallen37 [ Wed Mar 21, 2012 9:44 pm ]
Post subject:  Re: Converting a string into a byte array

Right, strlen will get the length of a string, but how do you set the array to contain that many elements?

Author:  NeXT-Generation [ Wed Mar 21, 2012 9:51 pm ]
Post subject:  Re: Converting a string into a byte array

mattallen37 wrote:
Right, strlen will get the length of a string, but how do you set the array to contain that many elements?

I think that it would be this:

Code:
byte baz[strlen(foo)];


Not positive, as I haven't manipulated strings much, but I don't see why it wouldn't work.

Author:  mattallen37 [ Wed Mar 21, 2012 10:43 pm ]
Post subject:  Re: Converting a string into a byte array

I would think so to, but the compiler doesn't like it. Furthermore, I want to resize it after it has been declared (like if it's a global array that changes size throughout the program).

Author:  magicode [ Wed Mar 21, 2012 11:59 pm ]
Post subject:  Re: Converting a string into a byte array

Try using the sizeof() function.

Author:  mattallen37 [ Thu Mar 22, 2012 12:00 am ]
Post subject:  Re: Converting a string into a byte array

That's for reading the length of an array, not setting it.

Author:  magicode [ Thu Mar 22, 2012 12:02 am ]
Post subject:  Re: Converting a string into a byte array

Yes, I know. I meant:
Code:
byte baz[sizeof(foo)];

Author:  mattallen37 [ Thu Mar 22, 2012 12:10 am ]
Post subject:  Re: Converting a string into a byte array

I don't think that would work. I tried passing an int, as well as a byte, and const. Nothing worked at initialization, except for literals (hard coded numbers).

Also, sizeof with a string doesn't work properly. It seems that strings are always a certain length (20 bytes), and so sizeof gets confused (and I think it always returns 20). Which brings up another issue; how do you use strings of more than 19 characters?

Author:  MHTS [ Fri Mar 30, 2012 6:55 pm ]
Post subject:  Re: Converting a string into a byte array

When declaring an array, you must be able to determine the size statically during compile time. If the size cannot be determine during compile time, you need to allocate the array dynamically during run-time. In ANSI C/C++, that would be the new operator (or malloc) but since RobotC is not ANSI C, I have no idea how you would do that. Is your string length fixed at compile time? If so sizeof() would work. Otherwise, you are out of luck.

Author:  mightor [ Sat Mar 31, 2012 1:27 am ]
Post subject:  Re: Converting a string into a byte array

non-const string variables are 20 bytes long; 19 characters, followed by a NULL (0). const strings can be longer, I forget what the max length was but it's significantly more. The reason is that ROBOTC does not support dynamically allocated memory.
strlen basically counts a along a string until it sees a NULL (0). sizeof() just looks at the total size of the variable, which, in the case of a non-const string, is always 20.
ROBOTC does not have malloc or free or any other memory management of that kind.

- Xander

Author:  mattallen37 [ Sat Mar 31, 2012 11:36 am ]
Post subject:  Re: Converting a string into a byte array

Thanks for the info!

I guess this is an area where NXC is far better.

Author:  Spiked3 [ Sat Mar 31, 2012 2:53 pm ]
Post subject:  Re: Converting a string into a byte array

mattallen37 wrote:
Thanks for the info!

I guess this is an area where NXC is far better.


Indeed, try debugging.

Page 1 of 2 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/