简体   繁体   English

Treating a character array as an integer - Learn C the Hard Way 额外学分

[英]Treating a character array as an integer - Learn C the Hard Way Extra credit

In Zed Shaw's "Learn C the Hard Way", exercise 9 ( http://c.learncodethehardway.org/book/ex9.html ) there is an extra credit question that I find interesting.在 Zed Shaw 的“艰难地学习 C”练习 9 ( http://c.learncodethehardway.org/book/ex9.html ) 中,有一个额外的学分问题我觉得很有趣。 He defines a 4-character array and asks the reader to figure out how to use the array as a 4-byte integer.他定义了一个 4 字符数组,并要求读者弄清楚如何将该数组用作 4 字节 integer。

At this point I know just enough to be dangerous, and I was thinking the answer is something along these lines:在这一点上,我知道足够危险了,我想答案是这样的:

#include <stdio.h>

int main(int argc, char *argv[])
{
    char name[4] = {'A'};

    int *name_int;
    name_int = &name;
    printf("%d", *name_int);

    return 0;
}

My thoughts being that if I created an int pointer with a value being the address of the array that the int type would use the byte of data in that address, followed by the next 3 bytes of data available.我的想法是,如果我创建一个值为数组地址的 int 指针,则 int 类型将使用该地址中的数据字节,然后是接下来的 3 个可用数据字节。 In my limited understanding, I am under the impression that both an int and an array would use memory in the same way: starting at an arbitrary memory address than using the next address in sequence, and so on.在我有限的理解中,我的印象是 int 和数组都会以相同的方式使用 memory:从任意 memory 地址开始,而不是按顺序使用下一个地址,依此类推。

However, the output of this isn't what I expected: I get the ascii value of 'A'.但是,这不是我所期望的 output:我得到了“A”的 ascii 值。 Which to me seems to indicate that my solution is incorrect, my understanding how memory is handled is incorrect, or both.在我看来,这似乎表明我的解决方案不正确,我对 memory 处理方式的理解不正确,或两者兼而有之。

How can this little hack be accomplished and where is it I am going wrong?如何完成这个小技巧,我哪里出错了? I am hoping to walk away from this with a better understanding of how pointers and references work, and how memory is stored and used.我希望通过更好地理解指针和引用的工作原理以及 memory 的存储和使用方式来摆脱这一点。

Thank you!谢谢!

You are running into little-endian vs big-endian representation of numbers. 你正在遇到little-endian vs big-endian数字表示。

Let's take a look at the values of 4-btyes used to represent a 4-byte integer. 我们来看看用于表示4字节整数的4-btyes的值。

+----+----+----+----+
| N1 | N2 | N3 | N4 |
+----+----+----+----+

In a big-endian representation, these 4 bytes represent: 在big-endian表示中,这4个字节表示:

N1*2^24 + N2*2^16 + N3*2^8 + N4

In a little-endian representation, those 4 bytes represent: 在小端表示中,这4个字节表示:

N1 + N2*2^8 + N3*2^16 + N4*2^24

In your case. 在你的情况下。

N1 = 'A' (65 decimal)
N2 = 0
N3 = 0
N4 = 0

Since the value of integer you are getting is 65 , you have a little endian representation. 由于你得到的整数值是65 ,你有一个小的endian表示。 If you want to treat those numbers like a big-endian representation, you can use the following: 如果您想将这些数字视为big-endian表示,则可以使用以下内容:

#include <stdio.h>

int main(int argc, char *argv[])
{
   int i;
   char nameString[4] = {'A'};
   int name = 0;

   for ( i = 0; i < 4; ++i )
   {
      name = (name << 8) + nameString[i];
   }

   printf("%d\n", name);
   printf("%X\n", name);

   return 0;
}

The output I get with the above code: 我得到的输出与上面的代码:

1090519040
41000000

You may also try the function memcpy() .您也可以尝试 function memcpy()

Use a char array as a source and an unassigned int variable as the destination.使用 char 数组作为源,使用未分配的 int 变量作为目标。

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

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