2

Is the behavior of escape characters compiler dependent or something?

std::string pattern = "\xDDAF5742"; // or do pattern("\xDDAF5742");
std::cout << pattern << " " << pattern.size() << "\n";

On my system this outputs B 1 ('B' == 0x42), but I thought it should put 4 characters into the string.

Matt Munson
  • 2,903
  • 5
  • 33
  • 52
  • http://stackoverflow.com/questions/5784969/when-did-c-compilers-start-considering-more-than-two-hex-digits-in-string-lite – billz Jul 02 '14 at 02:27

1 Answers1

2

You want this:

std::string pattern = "\xDD\xAF\x57\x42"; 

Otherwise, it tries to read your entire hex code in as one char, which then is truncated to only the last 8 bits.

Red Alert
  • 3,786
  • 2
  • 17
  • 24
  • ok, cause I read here in the comment http://stackoverflow.com/a/10220539/574733 that you can do it the way I did above. I guess he was wrong, even though he got 9 upvotes? Or, on second though, I probly just misinterpreted. – Matt Munson Jul 02 '14 at 02:19
  • 1
    @MattMunson that comment is true. It states that it _reads_ characters untill the first non-hex value, not _interprets_. That's a big difference :) – Paweł Stawarz Jul 02 '14 at 02:22
  • "truncated to only the last 8 bits" is implementation defined ("The escape `\xhhh` consists of the backslash followed by `x` followed by one or more hexadecimal digits that are taken to specify the value of the desired character. There is no limit to the number of digits in a hexadecimal sequence. A sequence of octal or hexadecimal digits is terminated by the first character that is not an octal digit or a hexadecimal digit, respectively. The value of a character literal is implementation-defined if it falls outside of the implementation-defined range defined for `char`...") – Tony Delroy Jul 02 '14 at 02:23