简体   繁体   English

字符混乱中的转义序列

[英]Escape sequence in char confusion

I came across this example here . 我在这里遇到了这个例子。

#include <iostream>

int main()
{
    int i = 7;

    char* p = reinterpret_cast<char*>(&i);
    if(p[0] == '\x7') //POINT OF INTEREST
        std::cout << "This system is little-endian\n";
    else
        std::cout << "This system is big-endian\n";
}

What I'm confused about is the if statement. 我感到困惑的是if语句。 How do the escape sequences behave here? 转义序列在这里的表现如何? I get the same result with p[0] == '\\07' ( \\x being hexadecimal escape sequence). 我得到与p[0] == '\\07'\\x为十六进制转义序列)相同的结果。 How would checking if p[0] == '\\x7' tell me if the system is little or big endian? 如何检查p[0] == '\\x7'告诉我系统的字节序是小端还是大端?

Hex 7 and octal 7 happens to be the same value, as is decimal 7. 十六进制7和八进制7恰好是相同的值,十进制7也是如此。

The check is intended to try to determine if the value ends up in the first or last byte of the int . 该检查旨在尝试确定该值以int的第一个字节还是最后一个字节结尾。

A little endian system will store the bytes of the value in "reverse" order, with the lower part first 小字节序系统将以“反向”顺序存储值的字节,低位在前

07 00 00 00

A big endian system would store the "big" end first 大端系统将首先存储“大”端

00 00 00 07

By reading the first byte, the code will see if the 7 ends up there, or not. 通过读取第一个字节,代码将查看7是否结束。

The layout of a (32-bit) integer in memory is; (32位)整数在内存中的布局为;

Big endian:

+-----+-----+-----+-----+
|  0  |  0  |  0  |  7  |
+-----+-----+-----+-----+
   ^ pointer to int points here

Little endian:

+-----+-----+-----+-----+
|  7  |  0  |  0  |  0  |
+-----+-----+-----+-----+
   ^ pointer to int points here

What the code basically does is read the first char that the integer pointer points to, which in case of little endian is \\x0 , and big endian is \\x7 . 该代码基本上执行的操作是读取整数指针指向的第一个char ,在小端字节序为\\x0情况下,在大端字节序为\\x7

7 in decimal is the same as 7 in hexadecimal and 7 in octal, so it doesn't matter if you use '\\x7' , '\\07' , or even just 7 (numeric literal, not a character one). 十进制的7与十六进制的7和八进制的7相同,因此使用'\\x7''\\07'或什至只使用7 (数字文字,而不是字符)都没有关系。

As for the endianness test: the value of i is 7, meaning it will have the number 7 in its least significant byte, and 0 in all other bytes. 至于字节序测试: i值为7,这意味着它将在其最低有效字节中具有数字7,在所有其他字节中具有数字0。 The cast char* p = reinterpret_cast<char*>(&i); 强制转换char* p = reinterpret_cast<char*>(&i); makes p point to the first byte in the representation of i . 使p指向i表示中的第一个字节。 The test then checks whether that byte's value is 7. If so, it's the least significant byte, implying a little-endian system. 然后,测试将检查该字节的值是否为7。如果是,则它是最低有效字节,这表示系统为低端字节序。 If the value is not 7, it's not a little-endian system. 如果该值不是7,则不是小端系统。 The code assumes that it's a big-endian, although that's not strictly established (I believe there were exotic systems with some sort of mixed endianness as well, although the code will probably not run on such in practice). 该代码假定它是一个大端序的,尽管并没有严格建立(我相信有些异国情调的系统也具有某种混合的端序,尽管实际上该代码可能不会在这种情况下运行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM