Char数组为C中无符号长整数

时间:2021-03-10 22:29:46

A bit of a theoretical question if that is exaptable: Recently I encounter this piece of code (little endian system):

一点理论上的问题,如果这是可行的:最近我遇到这段代码(小端系统):

char arr[] = {0x50, 0x60, 0x70, 0x80, 0x81, 0x82, 0x83, 0x84}; //arr size = 8
unsigned long long a = *((unsigned long long*)arr);

The value of a after this 2 rows is 9548619127672823888, and I'm having a hard time understanding why (as to the casting process), would highly appriciate if someone may offer me some help.

这2行之后的a值是9548619127672823888,而我很难理解为什么(关于演员流程),如果有人可以给我一些帮助的话会很高兴。

thanks a lot!

非常感谢!

2 个解决方案

#1


2  

Well, you need to understand how numbers are stored in memory. Take number 0x12345678 written in hex. How would you store it in memory? Let's take little endian system. This number would be written in the following way:

那么,您需要了解数字如何存储在内存中。以十六进制写的数字0x12345678。你会如何将它存储在内存中?让我们采用小端系统。这个数字将按以下方式编写:

Memory Address      Value
1000                0x78 (Least significant byte goes first)
1001                0x56
1002                0x34
1003                0x12

Now, if this is little endian system. How would you assemble the integer back from these values and address? Easily, since this is little endian system, we know the first byte stored at the lowest address is least significant byte. Using this approach we will assemble: 0x12345678.

现在,如果这是小端系统。你会如何从这些值和地址汇集整数?很容易,因为这是小端系统,我们知道存储在最低地址的第一个字节是最低有效字节。使用这种方法我们将组装:0x12345678。


What you have in code is basically doing something similar (note: There might be strict aliasing rule violation here and you should not do this, I am just giving you explanation):

你在代码中所拥有的基本上是做类似的事情(注意:这里可能存在严格的别名规则违规,你不应该这样做,我只是给你解释):

unsigned long long a = *((unsigned long long*)arr);

basically it says: go to address arr, interpret the values stored there (the values stored there are the values you specified in array, note name of array decays to address of first element of array) as values of some integer - particularly of unsigned long long.

基本上它说:转到地址arr,解释存储在那里的值(存储在那里的值是你在数组中指定的值,注意数组的名称衰减到数组的第一个元素的地址)作为某个整数的值 - 特别是unsigned long的值长。

It goes, and depending on the endianness, assembles the integer out of those values then like we did in the example above.

然后,根据字节顺序,将这些值中的整数组合起来,就像我们在上面的例子中所做的那样。

#2


1  

arr in that context is a pointer to a char. (*arr would return a single char value.) So they first cast a pointer to a one-byte char to a pointer of the type that they want - a pointer to an 8-byte long. The pointer still refers to the same address, but will now cause different behavior when de-referenced - an 8-byte value gets returned instead of a one-byte value.

arr在该上下文中是指向char的指针。 (* arr将返回一个char值。)因此,他们首先将指向单字节char的指针转换为他们想要的类型的指针 - 指向8字节长的指针。指针仍然引用相同的地址,但现在会在取消引用时导致不同的行为 - 返回一个8字节的值而不是一个字节的值。

#1


2  

Well, you need to understand how numbers are stored in memory. Take number 0x12345678 written in hex. How would you store it in memory? Let's take little endian system. This number would be written in the following way:

那么,您需要了解数字如何存储在内存中。以十六进制写的数字0x12345678。你会如何将它存储在内存中?让我们采用小端系统。这个数字将按以下方式编写:

Memory Address      Value
1000                0x78 (Least significant byte goes first)
1001                0x56
1002                0x34
1003                0x12

Now, if this is little endian system. How would you assemble the integer back from these values and address? Easily, since this is little endian system, we know the first byte stored at the lowest address is least significant byte. Using this approach we will assemble: 0x12345678.

现在,如果这是小端系统。你会如何从这些值和地址汇集整数?很容易,因为这是小端系统,我们知道存储在最低地址的第一个字节是最低有效字节。使用这种方法我们将组装:0x12345678。


What you have in code is basically doing something similar (note: There might be strict aliasing rule violation here and you should not do this, I am just giving you explanation):

你在代码中所拥有的基本上是做类似的事情(注意:这里可能存在严格的别名规则违规,你不应该这样做,我只是给你解释):

unsigned long long a = *((unsigned long long*)arr);

basically it says: go to address arr, interpret the values stored there (the values stored there are the values you specified in array, note name of array decays to address of first element of array) as values of some integer - particularly of unsigned long long.

基本上它说:转到地址arr,解释存储在那里的值(存储在那里的值是你在数组中指定的值,注意数组的名称衰减到数组的第一个元素的地址)作为某个整数的值 - 特别是unsigned long的值长。

It goes, and depending on the endianness, assembles the integer out of those values then like we did in the example above.

然后,根据字节顺序,将这些值中的整数组合起来,就像我们在上面的例子中所做的那样。

#2


1  

arr in that context is a pointer to a char. (*arr would return a single char value.) So they first cast a pointer to a one-byte char to a pointer of the type that they want - a pointer to an 8-byte long. The pointer still refers to the same address, but will now cause different behavior when de-referenced - an 8-byte value gets returned instead of a one-byte value.

arr在该上下文中是指向char的指针。 (* arr将返回一个char值。)因此,他们首先将指向单字节char的指针转换为他们想要的类型的指针 - 指向8字节长的指针。指针仍然引用相同的地址,但现在会在取消引用时导致不同的行为 - 返回一个8字节的值而不是一个字节的值。