I get a String
like: XXXXXXXXX
Its length is fixed and 9.
I need to get an char array like:
char array[9] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
... from there on my sketch processes this array.
Maybe someone has an idea on how to achieve this. I thought about a for-statement to store every single letter in a string array. Then another for-statement to build an char array with the hex presentation of each string. Seems to be a bit complicated though I think.
Really appreciate your help.
EDIT:
What I need to do:
- Separate the string into letters / figures.
- Change every single letter to its HEX representation.
- Build a char Array out of the HEX values.
Example:
- 123 (String)
- 0x310x320x33 (String or whatever ...)
- {0x31,0x32,0x33} (Char-Array)
This is what I need!
3 Answers 3
Sounds like the following string.toCharArray(buf, len) is what you want. as exampled
String stringOne = "123456789";
char charBuf[50];
stringOne.toCharArray(charBuf, 50) ;
Serial.println(stringOne);
for (int i = 0; i < 9; i++) {
Serial.print(" 0x");
Serial.print(charBuf[i], HEX);
}
Serial.println();
yields:
123456789
0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39
as each contents of charBuf is an uint8_t, regardless of how it is printed.
Unless you are asking for each element of the array to be another string of the ASCII representation of the HEX. or are you asking to have a char array of "0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39"
-
\$\begingroup\$ Already used that. The point is, that the Array then looks like
{0x00,0x00, ... , X,X,X,X,X,X}
, so the string is not converted to HEX. P.S. I have a static char (HEX) array in front of myI need to convert the string to HEX-
Array \$\endgroup\$DAS– DAS2013年02月08日 15:28:47 +00:00Commented Feb 8, 2013 at 15:28 -
\$\begingroup\$ I edited my question. \$\endgroup\$DAS– DAS2013年02月08日 15:36:35 +00:00Commented Feb 8, 2013 at 15:36
-
1\$\begingroup\$ @Darwin
string.toCharArray
will do exactly what your example shows. \$\endgroup\$George– George2013年02月08日 15:41:34 +00:00Commented Feb 8, 2013 at 15:41 -
\$\begingroup\$ keep in mind that char array is an array of integers. Not HEX nor DEC. \$\endgroup\$mpflaga– mpflaga2013年02月08日 15:47:25 +00:00Commented Feb 8, 2013 at 15:47
-
\$\begingroup\$ Well ... you are right. My project now works, did not expect that for today. Thanks a lot! Made things a lot easier! \$\endgroup\$DAS– DAS2013年02月08日 16:06:29 +00:00Commented Feb 8, 2013 at 16:06
If your string variable is of Arduino type String, you can use a toCharArray() method to get an array of chars.
char array[9];
yourString.toCharArray(array, 9);
-
\$\begingroup\$ Maybe my question was not precise ... I have a String. The string must be separated into a array. Each child of the array must be converted to HEX. \$\endgroup\$DAS– DAS2013年02月08日 15:31:49 +00:00Commented Feb 8, 2013 at 15:31
-
\$\begingroup\$ hex is just a representation of a number. If you want them output in hex, use HEX argument in call to Serial.print. Like so: Serial.print(array[i], HEX) \$\endgroup\$miceuz– miceuz2013年02月08日 17:02:11 +00:00Commented Feb 8, 2013 at 17:02
If You Have String
String item= "Hello";
AUX3= item.charAt(0);
AUX3 is 0x48 in Hex, 72 in Dec
if (AUX3==72) {
Answer1();
}
-
1\$\begingroup\$ Welcome to EE.SE! I'm not sure your answer effectively answers the question, since the asker needs an array, not just the values in the string. Please consider improving your answer if you have something to contribute in addition to the current answers. \$\endgroup\$user2943160– user29431602016年07月18日 00:11:02 +00:00Commented Jul 18, 2016 at 0:11