简体   繁体   English

C ++将整数复制到char []或无符号char []错误

[英]C++ copying integer to char[] or unsigned char[] error

So I'm using the following code to put an integer into a char[] or an unsigned char[] 因此,我正在使用以下代码将整数放入char []或无符号char []中

(unsigned???) char test[12];

test[0] = (i >> 24) & 0xFF;
test[1] = (i >> 16) & 0xFF;
test[2] = (i >> 8) & 0xFF;
test[3] = (i >> 0) & 0xFF;

int j = test[3] + (test[2] << 8) + (test[1] << 16) + (test[0] << 24);

printf("Its value is...... %d", j);

When I use type unsigned char and value 1000000000 it prints correctly. 当我使用无符号字符类型和值1000000000时,它可以正确打印。

When I use type char (same value) I get 98315724 printed? 当我使用char类型(相同的值)时,会得到98315724的打印内容吗?

So, the question really is can anyone explain what the hell is going on?? 所以,问题是,真的有人可以解释到底发生了什么吗?


Upon examining the binary for the two different numbers I still can't work out whats going on. 在检查了两个不同数字的二进制文件后,我仍然无法弄清楚到底发生了什么。 I thought signed was when the MSB was set to 1 to indicate a negative value (but negative char? wth?) 我认为带符号的是当MSB设置为1来指示负值时(但是负char?wth?)。

I'm explicitly telling the buffer what to insert into it, and how to interpret the contents, so don't see why this could be happening. 我明确告诉缓冲区要插入什么内容,以及如何解释内容,所以不明白为什么会发生这种情况。

I have included binary/hex below for clarity in what I examined. 为了清楚起见,我在下面包括了二进制/十六进制。

11 1010 1001 1001 1100 1010 0000 0000 // Binary for 983157248 11 1010 1001 1001 1100 1010 0000 0000 //二进制为983157248

11 1011 1001 1010 1100 1010 0000 0000 // Binary for 1000000000 11 1011 1001 1010 1100 1010 0000 0000 // 1000000000的二进制

3 A 9 9 CA 0 0 // Hex for 983157248 3 A 9 9 CA 0 0 //十六进制为983157248

3 B 9 ACA 0 0 // Hex for 1000000000 3 B 9 ACA 0 0 //十六进制为1000000000

When you say i & 0xFF etc, you're creaing values in the range [0, 256) . 当你说i & 0xFF等等,你在范围creaing值[0, 256) But (your) char has a range of [-128, +128) , and so you cannot actually store those values sensibly (ie the behaviour is implementation defined and tedious to reason about). 但是,(您的) char的范围为[-128, +128) ,因此您实际上不能明智地存储这些值(即,行为是实现定义的,并且难以理解)。

Use unsigned char for unsigned values. 使用unsigned char作为无符号值。 The clue is in the name. 线索就是名字。

In addition to the answer by Kerrek SB please consider the following: 除了Kerrek SB的答案以外,请考虑以下事项:

Computers (almost always) use something called twos-complement notation for negative numbers, with the high bit functioning as a 'negative' indicator. 计算机(几乎总是)对负数使用称为二进制补码的表示法 ,其中高位用作“负”指示符。 Ask yourself what happens when you perform shifts on a signed type considering that the computer will handle the signed bit specially. 问自己,考虑到计算机将专门处理带符号位,对带符号类型执行移位时会发生什么。

You may want to read Why does left shift operation invoke Undefined Behaviour when the left side operand has negative value? 您可能需要阅读为什么当左侧操作数的值为负时,为什么左移操作会调用Undefined Behavior? right here on StackOverflow for a hint. 就在StackOverflow上获得提示。

This all has to do with internal representation and the way each type uses that data to interpret it. 所有这些都与internal representation以及每种type使用该data解释它的方式有关。 In the internal representation of a signed character , the first bit of your byte holds the sign, the others, the value. 在带signed characterinternal representation中,字节的第一位保留符号,其余部分保留值。 when the first bit is 1, the number is negative, the following bits then represent the complement of the positive value. 当第一位为1时,数字为负,随后的位代表正值的complement for example: 例如:

unsigned char c;  // whose internal representation we will set at 1100 1011
c = (1 * 2^8) + (1 * 2^7) + (1 * 2^4) + (1 * 2^2) + (1 * 2^1);
cout << c;        // will give 203

                  // inversely:

char d = c;       // not unsigned
cout << d;        // will print -53
                  // as if the first is 1, d is negative, 
                  // and other bits complement of value its positive value
                  // 1100 1011  -> -(complement of 100 1011)
                  // the complement is an XOR +1   011 0101

                  // furthermore:

char e;           // whose internal representation we will set at 011 0101
e = (1 * 2^6) + (1 * 2^5) + (1 * 3^2) + (1 * 2^1);
cout << e;        // will print 53

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

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