1
\$\begingroup\$

I am trying to create a string with characters outside the ASCII printable range. I am using an STM32 microcontroller using the µVision IDE with the Keil compiler. The documentation allows various escape sequences, including one for "ASCII code in hexadecimal." When I just want one character there is no problem, but if I want to put other numbers in the same string after the character, I start getting issues.

This works.

char myString[10] = "\x06";//(It gives me 0x06,0x00,...,0x00)

This doesn't. (It gives me 0x60

char myString[10] = "\x060";//(It gives me 0x60,0x00,...,0x00)

The more characters (0-F) in the same continuity that keep getting appended as hexadecimal digits of the same number (it will even throw a warning that it is out of range if the number gets too big). Is there some way around this? Might this be a compiler issue?

\$\endgroup\$
1
\$\begingroup\$

If you are asking how to terminate a hex escape sequence when followed by a character which is a hex digit, here is a way

"\x06""0"

Use two double-quotes.

Otherwise, a hex escape sequence will use up all consecutive hex digits that follow.

\$\endgroup\$
1
\$\begingroup\$

When you initialize a string like that, you are specifying one character at a time. One character can only hold a value between 0 and 255. If you want to fill more bytes of your string, just specify more characters in the initializer:

char myString[4] = "\x06\x60\x44";

Note that unspecified characters are not guaranteed to be filled with 0's, although one 0 will be placed at the end of whatever you specify as the initializer:

char string2[8] = "test";

Five characters will be initialized, characters 6 through 7 might contain garbage.

\$\endgroup\$
  • 2
    \$\begingroup\$ It would be more accurate to say that a string literal specifies one char at a time. The OP did not say what encoding he's using, but in some encodings, there can be more than one char per character. \$\endgroup\$ – Solomon Slow Apr 27 '15 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.