Author |
Message |
mattallen37
Expert
Joined: Thu Sep 29, 2011 11:09 pm Posts: 184 Location: Michigan USA
|
 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).
_________________ Matt
|
Wed Mar 21, 2012 5:01 pm |
|
 |
mightor
Site Admin
Joined: Wed Mar 05, 2008 8:14 am Posts: 3654 Location: Rotterdam, The Netherlands
|
 Re: Converting a string into a byte array
I would usually just memcpy() it  Edit: Something along the lines of: - Xander
_________________| Professional Conduit of Reasonableness| (Title bestowed upon on the 8th day of November, 2013) | My Blog: I'd Rather Be Building Robots| ROBOTC 3rd Party Driver Suite: [ Project Page]
|
Wed Mar 21, 2012 5:49 pm |
|
 |
mattallen37
Expert
Joined: Thu Sep 29, 2011 11:09 pm Posts: 184 Location: Michigan USA
|
 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?
_________________ Matt
|
Wed Mar 21, 2012 8:53 pm |
|
 |
NeXT-Generation
Senior Roboticist
Joined: Wed Sep 28, 2011 10:13 pm Posts: 630 Location: If I told you, I'd have to kill you.
|
 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?
_________________A.K.A. inxt-generation Self-proclaimed genius, and future world dominator. My Brickshelf Folder"Don't they teach recreational mathematics anymore?" - The Tenth Doctor Bow down to Nikola Tesla, King of the Geek Gods.
|
Wed Mar 21, 2012 9:07 pm |
|
 |
mattallen37
Expert
Joined: Thu Sep 29, 2011 11:09 pm Posts: 184 Location: Michigan USA
|
 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?
_________________ Matt
|
Wed Mar 21, 2012 9:44 pm |
|
 |
NeXT-Generation
Senior Roboticist
Joined: Wed Sep 28, 2011 10:13 pm Posts: 630 Location: If I told you, I'd have to kill you.
|
 Re: Converting a string into a byte array
I think that it would be this: Not positive, as I haven't manipulated strings much, but I don't see why it wouldn't work.
_________________A.K.A. inxt-generation Self-proclaimed genius, and future world dominator. My Brickshelf Folder"Don't they teach recreational mathematics anymore?" - The Tenth Doctor Bow down to Nikola Tesla, King of the Geek Gods.
|
Wed Mar 21, 2012 9:51 pm |
|
 |
mattallen37
Expert
Joined: Thu Sep 29, 2011 11:09 pm Posts: 184 Location: Michigan USA
|
 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).
_________________ Matt
|
Wed Mar 21, 2012 10:43 pm |
|
 |
magicode
Moderator
Joined: Tue Sep 14, 2010 9:19 pm Posts: 496
|
 Re: Converting a string into a byte array
Try using the sizeof() function.
_________________ sudo rm -rf /
|
Wed Mar 21, 2012 11:59 pm |
|
 |
mattallen37
Expert
Joined: Thu Sep 29, 2011 11:09 pm Posts: 184 Location: Michigan USA
|
 Re: Converting a string into a byte array
That's for reading the length of an array, not setting it.
_________________ Matt
|
Thu Mar 22, 2012 12:00 am |
|
 |
magicode
Moderator
Joined: Tue Sep 14, 2010 9:19 pm Posts: 496
|
 Re: Converting a string into a byte array
_________________ sudo rm -rf /
|
Thu Mar 22, 2012 12:02 am |
|
 |
mattallen37
Expert
Joined: Thu Sep 29, 2011 11:09 pm Posts: 184 Location: Michigan USA
|
 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?
_________________ Matt
|
Thu Mar 22, 2012 12:10 am |
|
 |
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1523
|
 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.
|
Fri Mar 30, 2012 6:55 pm |
|
 |
mightor
Site Admin
Joined: Wed Mar 05, 2008 8:14 am Posts: 3654 Location: Rotterdam, The Netherlands
|
 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
_________________| Professional Conduit of Reasonableness| (Title bestowed upon on the 8th day of November, 2013) | My Blog: I'd Rather Be Building Robots| ROBOTC 3rd Party Driver Suite: [ Project Page]
|
Sat Mar 31, 2012 1:27 am |
|
 |
mattallen37
Expert
Joined: Thu Sep 29, 2011 11:09 pm Posts: 184 Location: Michigan USA
|
 Re: Converting a string into a byte array
Thanks for the info!
I guess this is an area where NXC is far better.
_________________ Matt
|
Sat Mar 31, 2012 11:36 am |
|
 |
Spiked3
Expert
Joined: Tue Feb 28, 2012 3:10 pm Posts: 197
|
 Re: Converting a string into a byte array
_________________Mike aka Spiked3 http://www.spiked3.com
|
Sat Mar 31, 2012 2:53 pm |
|
|