ASCII字符串和字节序

ASCII字符串和字节序

本文介绍了ASCII字符串和字节序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁与我工作的实习生给我看了他在计算机科学中采取字节排列顺序问题的考试。有一个问题,显示一个ASCII字符串我的比萨饼,学生必须证明怎么说字符串将被重新在内存psented小端计算机上$ P $。当然,这听起来像一个很难回答的问题,因为ASCII字符串不会受到endian的问题。

An intern who works with me showed me an exam he had taken in computer science about endianness issues. There was a question that showed an ASCII string "My-Pizza", and the student had to show how that string would be represented in memory on a little endian computer. Of course, this sounds like a trick question because ASCII strings are not affected by endian issues.

但令人震惊的,实习生要求他的坚持教授,该字符串将被重新psented为$ P $:

But shockingly, the intern claims his professor insists that the string would be represented as:

P-yM azzi

我知道这不可能是正确的。有没有办法ASCII字符串将重新psented像任何计算机上$ P $。但很显然,这位教授是坚持这一点。所以,我写了一个小的C程序,并告诉实习生把它给他的教授。

I know this can't be right. There is no way an ASCII string would be represented like that on any machine. But apparently, the professor is insisting on this. So, I wrote up a small C program and told the intern to give it to his professor.

#include <string.h>
#include <stdio.h>

int main()
{
    const char* s = "My-Pizza";
    size_t length = strlen(s);
    for (const char* it = s; it < s + length; ++it) {
        printf("%p : %c\n", it, *it);
    }
}

这清楚地表明,该字符串存储在内存中我的比萨饼。一天后,实习生回来给我,告诉我的教授现在声称C被自动地转换地址,以显示正确的顺序字符串。

This clearly demonstrates that the string is stored as "My-Pizza" in memory. A day later, the intern gets back to me and tells me the professor is now claiming that C is automagically converting the addresses to display the string in proper order.

我告诉他,他的教授是疯狂的,这显然是错误的。但就在这里检查我自己的理智,我决定发布此计算器上的,所以我可以让别人来确认我在说什么。

I told him his professor is insane, and this is clearly wrong. But just to check my own sanity here, I decided to post this on stackoverflow so I could get others to confirm what I'm saying.

所以,我要问:是谁在这里

So, I ask : who is right here?

推荐答案

毫无疑问,你是正确的。

Without a doubt, you are correct.

ANSI C标准6.1.4规定由字符串存储在内存中串联中的文字字符。

ANSI C standard 6.1.4 specifies that string literals are stored in memory by "concatenating" the characters in the literal.

ANSI标准6.3.6还规定了另外的一个指针值的效果:

ANSI standard 6.3.6 also specifies the effect of addition on a pointer value:

当具有整体式的前pression加到或从一个指针中减去,其结果具有指针操作数的类型。如果指针操作数指向数组对象的元素,并且阵列足够大,结果指向一个元件从原始元件偏移,使得所得到的和原始的数组元素的下标之差等于积分前pression。

如果归因于这个人的想法是正确的,那么编译器将也有猴子周围的整数运算时,整数作为数组的索引。许多其他的谬误也会导致这些留下来的想象空间。

If the idea attributed to this person were correct, then the compiler would also have to monkey around with integer math when the integers are used as array indices. Many other fallacies would also result which are left to the imagination.

的人,可能会混淆,因为(不像一个字符串初始化),多字节chacter常量,如ABCD的的存储endian顺序。

The person may be confused, because (unlike a string initializer), multi-byte chacter constants such as 'ABCD' are stored in endian order.

有很多原因的人可能会搞不清楚这一点。正如其他人在这里建议,他可能会被误读,他看到了一个调试器窗口,其中的内容已经字节交换对于int值的可读性的东西。

There are many reasons a person might be confused about this. As others have suggested here, he may be misreading what he sees in a debugger window, where the contents have been byte-swapped for readability of int values.

这篇关于ASCII字符串和字节序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:57