4
\$\begingroup\$

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:

  1. Separate the string into letters / figures.
  2. Change every single letter to its HEX representation.
  3. Build a char Array out of the HEX values.

Example:

  1. 123 (String)
  2. 0x310x320x33 (String or whatever ...)
  3. {0x31,0x32,0x33} (Char-Array)

This is what I need!

asked Feb 8, 2013 at 15:04
\$\endgroup\$

3 Answers 3

7
\$\begingroup\$

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"

answered Feb 8, 2013 at 15:25
\$\endgroup\$
5
  • \$\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 my I need to convert the string to HEX-Array \$\endgroup\$ Commented Feb 8, 2013 at 15:28
  • \$\begingroup\$ I edited my question. \$\endgroup\$ Commented Feb 8, 2013 at 15:36
  • 1
    \$\begingroup\$ @Darwin string.toCharArray will do exactly what your example shows. \$\endgroup\$ Commented Feb 8, 2013 at 15:41
  • \$\begingroup\$ keep in mind that char array is an array of integers. Not HEX nor DEC. \$\endgroup\$ Commented 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\$ Commented Feb 8, 2013 at 16:06
2
\$\begingroup\$

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);
answered Feb 8, 2013 at 15:27
\$\endgroup\$
2
  • \$\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\$ Commented 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\$ Commented Feb 8, 2013 at 17:02
0
\$\begingroup\$

If You Have String

String item= "Hello";

AUX3= item.charAt(0);

AUX3 is 0x48 in Hex, 72 in Dec

 if (AUX3==72) {
 Answer1(); 
 }
answered Jul 17, 2016 at 21:56
\$\endgroup\$
1
  • 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\$ Commented Jul 18, 2016 at 0:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.