View unanswered posts | View active topics It is currently Fri Jul 25, 2014 5:20 am






Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Converting a string into a byte array 
Author Message
Expert

Joined: Thu Sep 29, 2011 11:09 pm
Posts: 184
Location: Michigan USA
Post 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
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3163
Location: Rotterdam, The Netherlands
Post 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

_________________
| 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
Profile WWW
Expert

Joined: Thu Sep 29, 2011 11:09 pm
Posts: 184
Location: Michigan USA
Post 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
Profile WWW
Senior Roboticist
User avatar

Joined: Wed Sep 28, 2011 10:13 pm
Posts: 630
Location: If I told you, I'd have to kill you.
Post 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
Profile WWW
Expert

Joined: Thu Sep 29, 2011 11:09 pm
Posts: 184
Location: Michigan USA
Post 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
Profile WWW
Senior Roboticist
User avatar

Joined: Wed Sep 28, 2011 10:13 pm
Posts: 630
Location: If I told you, I'd have to kill you.
Post 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.

_________________
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
Profile WWW
Expert

Joined: Thu Sep 29, 2011 11:09 pm
Posts: 184
Location: Michigan USA
Post 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
Profile WWW
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Converting a string into a byte array
Try using the sizeof() function.

_________________
sudo rm -rf /


Wed Mar 21, 2012 11:59 pm
Profile
Expert

Joined: Thu Sep 29, 2011 11:09 pm
Posts: 184
Location: Michigan USA
Post 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
Profile WWW
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Converting a string into a byte array
Yes, I know. I meant:
Code:
byte baz[sizeof(foo)];

_________________
sudo rm -rf /


Thu Mar 22, 2012 12:02 am
Profile
Expert

Joined: Thu Sep 29, 2011 11:09 pm
Posts: 184
Location: Michigan USA
Post 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
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post 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
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3163
Location: Rotterdam, The Netherlands
Post 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
Profile WWW
Expert

Joined: Thu Sep 29, 2011 11:09 pm
Posts: 184
Location: Michigan USA
Post 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
Profile WWW
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post 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.

_________________
Mike aka Spiked3
http://www.spiked3.com


Sat Mar 31, 2012 2:53 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 16 posts ]  Go to page 1, 2  Next

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.