Escape sequences and characters

Jul 15, 2013 at 6:04am
Hello, I am making my way through c++ primer (Lippman) and I have found the examples/exercise so far quite good at reinforcing the topics described.

In the escape sequences section, it talks about using hex (\x) or oct escape values to represent any character. The uses of this might be fairly limited, but I want to understand something about printing characters after a hex escape sequence -

std::cout << "Hello \x4d" << std::endl; \\ 4d in hex is 'M'

Hello M


std::cout << "Hello \x4dum" << std::endl;

Hello Mum


std::cout << "Hello \x4dad << std::endl;

error C2022: '19885' : too big for character


I understand that hexadecimal escape sequences have no length limit and terminate at the first character that is not a valid hexadecimal digit. But what about when the characters happen to be a valid hex digits?

I can see no way of printing the text literals "ad" after the hex escape sequence apart from

std::cout << "Hello \x4d" "ad" << std::endl;

How would you print "Hello Mad" with the "M" represented as hex, and "ad" as a string literal? (without having it in separate double quotes?) Or is that simply what you need to do in this situtation?

I tried putting in a null character in between ie "Hello \x4d\0ad" but that did not work.

My platform is windows, and my compiler is VS 2008. Thanks.
Last edited on Jul 15, 2013 at 6:14am
Jul 15, 2013 at 7:56am
Null character won't work, because it would terminate whole string, skipping content that is located after '\0'.

First off, I wouldn't use this too often, but if I had, I would take safe approach, using
 
std::cout<, "Hello \x4d" << "ad" << std::endl;

just as you said. It's simple, and effective. I don't think there is any need of looking for another way of solving this problem, if this one is sufficient.
Jul 15, 2013 at 9:18am
> Or is that simply what you need to do in this situtation?

Yes. we can just treat two consecutive double quotes "" as the terminator of an escape sequence.
std::cout << "Hello \x4d""ad 1\62""3\n" ;
Last edited on Jul 15, 2013 at 9:18am
Jul 15, 2013 at 10:28am
Just as a note, that works, I believe, because adjacent string literals are automatically concatenated.
Jul 15, 2013 at 2:48pm
Yep, and the quotes don't even need to be right next to each other. The preprocessor merges adjacent string literals irrespective of how much white space there is between them.

I also use this trick to lay out long strings in a similar way to how I want them to be displayed, esp this kind of thing:

1
2
3
4
5
6
const char msg[] = "Example Program Help\n"
                   "example [/v][msg]\n"
                   "example /h for this help\n"
                   "etc\n";

std::cout << msg;


Andy
Last edited on Jul 15, 2013 at 2:51pm
Jul 15, 2013 at 5:52pm
@Wux
How would you print "Hello Mad" with the "M" represented as hex, and "ad" as a string literal? (without having it in separate double quotes?) Or is that simply what you need to do in this situation?


Well, here's one way to do it. Notice I put a space after the hex number, then a back slash followed with a b. That is the escape sequence for a backspace. Now the text is printed next to the letter represented by the hex number.
1
2
std::cout << "Hello \x4d \bum" << std::endl;
std::cout << "I'm \x4d \bad" << std::endl;
Topic archived. No new replies allowed.