治疗一个字符数组作为整数 - 学习C坚硬方式附加题 [英] Treating a character array as an integer - Learn C the Hard Way Extra credit

查看:91
本文介绍了治疗一个字符数组作为整数 - 学习C坚硬方式附加题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在肖捷思锐的学习C辛苦,运动9( HTTP: //c.learn$c$cthehardway.org/book/ex9.html )还有就是我觉得有趣的额外信用的问题。他定义了一个4字符数组,要求读者弄清楚如何使用数组作为一个4字节的整数。

在这一点上,我知道刚够是危险的,我想答案是沿着这些路线的内容:

 的#include<&stdio.h中GT;INT主(INT ARGC,CHAR *的argv [])
{
    字符名称[4] = {'A​​'};    INT * name_int;
    name_int =放大器;名称;
    的printf(%d个,* name_int);    返回0;
}

我的想法是,如果我创建一个int终场前值作为数组int类型将使用该地址数据的字节,然后在接下来的3个字节可用数据的地址。在我有限的了解,我是IM pression下,无论一个int和数组会以同样的方式使用内存:开始不是使用序列中的下一个地址的任意内存地址,等等。

然而,这样做的输出不是我所期待的:我得到的'A'的ASCII值。这对我来说似乎表明我的解决方案是不正确,我的理解记忆是如何处理不正确,或两者兼而有之。

如何解决这个小黑客来完成,它在哪儿,我错了?我希望能从此更好地了解指针和引用是如何工作的走开,和记忆是如何存储和使用。

感谢您!


解决方案

您正在运行到小尾数VS数字大端再presentation。

让我们来看看在4 btyes用于重新present一个4字节整数的值。


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

在大端再presentation,这4个字节重新present:

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

在一个小端重新presentation,这4个字节重新present:

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

在你的情况。

  N1 ='A'(65进制)
N2 = 0
N3 = 0
N4 = 0

既然你得到整数值为 65 ,你有一点点端重新presentation。如果你要正确对待这些数字就像一个大端再presentation,可以使用以下内容:

 的#include<&stdio.h中GT;INT主(INT ARGC,CHAR *的argv [])
{
   INT I;
   焦炭nameString [4] = {'A​​'};
   INT名称= 0;   对于(I = 0; I&4; ++ⅰ)
   {
      名称=(名称<< 8)+ nameString [I]
   }   的printf(%d个\\ N,名);
   的printf(%X \\ n,名);   返回0;
}

输出我上述code得到:


1090519040
4100

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. He defines a 4-character array and asks the reader to figure out how to use the array as a 4-byte 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. 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.

However, the output of this isn't what I expected: I get the ascii value of 'A'. Which to me seems to indicate that my solution is incorrect, my understanding how memory is handled is incorrect, or both.

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.

Thank you!

解决方案

You are running into little-endian vs big-endian representation of numbers.

Let's take a look at the values of 4-btyes used to represent a 4-byte integer.

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

In a big-endian representation, these 4 bytes represent:

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

In a little-endian representation, those 4 bytes represent:

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. If you want to treat those numbers like a big-endian representation, you can use the following:

#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

这篇关于治疗一个字符数组作为整数 - 学习C坚硬方式附加题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆