number of digits in a hex escape code in C/C++

后端 未结 3 803
挽巷
挽巷 2021-01-07 23:10

I\'m having a dispute with a colleague of mine. She says that the following:

char* a = \"\\x000aaxz\";

will/can be seen by the compiler as

相关标签:
3条回答
  • 2021-01-07 23:49

    well, for that matter, hex escape sequence ends at a non hex character, e.g \x0abc9k is 0abc9 in hex and then 'k' so in order to end a hex sequence, you will have to use double quotes twice at the end of it e.g. \x0ab""c9k , which takes only 0ab as hex

    or alternately you could use octal escape sequence as there is limit to the numbers in octal escape sequence so there be maximum only three octal digits in it.. e.g. \o1234 is 123 in octal and then '4'

    So, yes, she is right.

    0 讨论(0)
  • 2021-01-07 23:51

    §2.13.2/4:

    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.

    She is right.

    However, you can terminate it early by eager catenation: the sequence of literals "\x000a" "axz" specifies a single four-character string literal. (2.13.4/3)

    Also note that Unicode uses 21-bit code points; it doesn't stop at 16 bits.

    0 讨论(0)
  • 2021-01-08 00:05

    Quote from MSDN on C++ character constants:

    Octal escape sequences, specified in the form \ooo, consist of a backslash and one, two, or three octal characters. Hexadecimal escape sequences, specified in the form \xhhh, consist of the characters \x followed by a sequence of hexadecimal digits. Unlike octal escape constants, there is no limit on the number of hexadecimal digits in an escape sequence.

    from http://msdn.microsoft.com/en-us/library/6aw8xdf2.aspx

    0 讨论(0)
提交回复
热议问题