C语言 \x 在 C/C++ 中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2547349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:00:07  来源:igfitidea点击:

What does \x mean in C/C++?

csyntax

提问by user198729

Example:

例子:

char arr[] = "\xeb\x2a";

BTW, are the following the same?

BTW,以下是一样的吗?

"\xeb\x2a"vs. '\xeb\x2a'

"\xeb\x2a"对比 '\xeb\x2a'

回答by Seth

\xindicates a hexadecimal character escape. It's used to specify characters that aren't typeable (like a null '\x00').

\x表示十六进制字符转义。它用于指定不可输入的字符(如 null '\x00')。

And "\xeb\x2a"is a literal string (type is char *, 3 bytes, null-terminated), and '\xeb\x2a'is a character constant (type is int, 2 bytes, not null-terminated, and is just another way to write 0xEB2A or 60202 or 0165452). Not the same :)

And"\xeb\x2a"是一个文字字符串(类型是char *3 个字节,以 null 结尾),并且'\xeb\x2a'是一个字符常量(类型是int2 个字节,不是以 null 结尾,只是另一种写 0xEB2A 或 60202 或 0165452 的方式)。不一样 :)

回答by Michael Burr

As other have said, the \xis an escape sequence that starts a "hexadecimal-escape-sequence".

正如其他人所说,这\x是一个开始“十六进制转义序列”的转义序列。

Some further details from the C99 standard:

C99 标准的更多细节:

When used inside a set of single-quotes (') the characters are part of an "integer character constant" which is (6.4.4.4/2 "Character constants"):

当在一组单引号 ( ') 中使用时,字符是“整数字符常量”的一部分,即 (6.4.4.4/2“字符常量”):

a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'.

用单引号括起来的一个或多个多字节字符的序列,如'x'.

and

An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined.

整数字符常量的类型为 int。包含映射到单字节执行字符的单个字符的整数字符常量的值是解释为整数的映射字符表示的数值。包含多个字符(例如,'ab')或包含不映射到单字节执行字符的字符或转义序列的整数字符常量的值是实现定义的。

So the sequence in your example of '\xeb\x2a'is an implementation defined value. It's likely to be the int value 0xeb2aor 0x2aebdepending on whether the target platform is big-endian or little-endian, but you'd have to look at your compiler's documentation to know for certain.

所以你的例子中的序列'\xeb\x2a'是一个实现定义的值。它可能是 int 值0xeb2a0x2aeb取决于目标平台是大端还是小端,但您必须查看编译器的文档才能确定。

When used inside a set of double-quotes (") the characters specified by the hex-escape-sequence are part of a null-terminated string literal.

当在一组双引号 ( ") 中使用时,由十六进制转义序列指定的字符是以空字符结尾的字符串文字的一部分。

From the C99 standard 6.4.5/3 "String literals":

来自 C99 标准 6.4.5/3“字符串文字”:

The same considerations apply to each element of the sequence in a character string literal or a wide string literal as if it were in an integer character constant or a wide character constant, except that the single-quote 'is representable either by itself or by the escape sequence \', but the double-quote "shall be represented by the escape sequence \".

相同的考虑适用于字符串字面量或宽字符串字面量中的序列的每个元素,就好像它在整数字符常量或宽字符常量中一样,除了单引号'可由其自身或转义表示sequence \',但双引号"应由转义序列 表示\"



Additional info:

附加信息:

In my opinion, you should avoid avoid using 'multi-character' constants. There are only a few situations where they provide any value over using an regular, old intconstant. For example, '\xeb\x2a'could be more portably be specified as 0xeb2aor 0x2aebdepending on what value you really wanted.

在我看来,您应该避免使用“多字符”常量。只有少数情况下它们比使用常规的旧int常量提供任何价值。例如,'\xeb\x2a'可以更便携地指定为0xeb2a0x2aeb取决于您真正想要的值。

One area that I've found multi-character constants to be of some use is to come up with clever enum values that can be recognized in a debugger or memory dump:

我发现多字符常量有用的一个领域是提出可以在调试器或内存转储中识别的巧妙枚举值:

enum CommandId {
    CMD_ID_READ  = 'read',
    CMD_ID_WRITE = 'writ',
    CMD_ID_DEL   = 'del ',
    CMD_ID_FOO   = 'foo '
};

There are few portability problems with the above (other than platforms that have small ints or warnings that might be spewed). Whether the characters end up in the enum values in little- or big-endian form, the code will still work (unless you're doing some else unholy with the enum values). If the characters end up in the value using an endianness that wasn't what you expected, it might make the values less easy to read in a debugger, but the 'correctness' isn't affected.

上述内容几乎没有可移植性问题(除了可能会喷出小整数或警告的平台)。无论字符以小端还是大端形式出现在枚举值中,代码仍然可以工作(除非您对枚举值做了一些其他不道德的事情)。如果字符最终使用的字节序不是您所期望的,则可能会使这些值在调试器中不太容易阅读,但“正确性”不会受到影响。

回答by Michael Burr

When you say:

当你说:

BTW,are these the same:

"\xeb\x2a" vs '\xeb\x2a'

顺便说一句,这些是一样的吗:

"\xeb\x2a" vs '\xeb\x2a'

They are in fact not. The first creates a character string literal, terminated with a zero byte, containing the two characters who's hex representation you provide. The second creates an integer constant.

他们实际上不是。第一个创建一个字符串文字,以零字节结尾,包含您提供的十六进制表示的两个字符。第二个创建一个整数常量。

回答by Loopo

\x allows you to specify the character by its hexadecimal code.

\x 允许您通过其十六进制代码指定字符。

This allows you to specify characters that are normally not printable (some of which have special escape sequences predefined such as '\n'=newline and '\t'=tab '\b'=bell)

这允许您指定通常不可打印的字符(其中一些具有预定义的特殊转义序列,例如 '\n'=newline 和 '\t'=tab '\b'=bell)

回答by Tarka

The \xmeans it's a hex character escape. So \xebwould mean character ebin hex, or 235in decimal. See http://msdn.microsoft.com/en-us/library/6aw8xdf2.aspxfor ore information.

\x意味着它是一个十六进制字符转义。所以\xeb将意味着eb十六进制或235十进制的字符。有关矿石信息,请参阅http://msdn.microsoft.com/en-us/library/6aw8xdf2.aspx

As for the second, no, they are not the same. The double-quotes, ", means it's a string of characters, a null-terminated character array, whereas a single quote, ', means it's a single character, the byte that character represents.

至于第二个,不,它们不一样。双引号 ,"表示它是一个字符串,一个以空字符结尾的字符数组,而单引号', 表示它是一个单个字符,即该字符所代表的字节。

回答by badcodenotreat

It's a special character that indicates the string is actually a hexadecimal number.

它是一个特殊字符,表示字符串实际上是一个十六进制数。

http://www.austincc.edu/rickster/COSC1320/handouts/escchar.htm

http://www.austincc.edu/rickster/COSC1320/handouts/escchar.htm

回答by SeargX

A useful website is here.

一个有用的网站在这里

And I quote:

我引用:

x Unsigned hexadecimal integer

x 无符号十六进制整数

That way, your \xebis like 235 in decimal.

这样,您的\xeb十进制数就像 235。