char数组在小端或大端系统中的顺序是否会有所不同 [英] Will a char array differ in ordering in a little endian or big endian system

查看:136
本文介绍了char数组在小端或大端系统中的顺序是否会有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组 char c [12] = {'a','b','c','d','e','f','g','h ','0','1','2','3'}
在十六进制中,这些值为 {0x61、0x62、0x63、0x64, 0x65、0x66、0x67、0x68、0x30、0x31、0x32、0x33}

我想知道的是数组是否将存储在

What I was wondering is whether the array would be stored in memory differently in a big endian or little endian system?

我认为它们是相同的,因为字节序系统通过确定如何以最少或最小的方式存储元素来工作。数组中单个元素的最高有效位并对字节进行排序,但是由于char只是单个字节,因此它们以相同的方式对字节进行排序。

I thought that they would be the same because the endian systems work by determining how to store an element by the least or most significant bits of a single element in the array and sorts the bytes, but since a char is just a single byte they order the bytes the same way.

推荐答案

考虑一下大端和小端的含义。

Think about what "big endian" and "little endian" means.

内存是一个字节序列(或更有趣的机器上是几个序列)。对于大小为两个或更多字节的项目,可以按不同顺序将项目的字节存储在内存中,并且两个最明显的顺序称为大字节序和小字节序(如果您有三个或更多字节) ,那么有两个以上可能的字节顺序,反正谁说例如int的位不能以混乱但固定的顺序分布在多个字节上?)

Memory is a sequence (or on more interesting machines, several sequences) of bytes. For items that are two or more bytes in size, the bytes of the item can be stored in memory in different orders, and the two most obvious orders are called "big endian" and "little endian" (if you have three or more bytes, then there are more than two possible byte orders, and anyway, who says that the bits of an int for example cannot be distributed over multiple bytes in a chaotic, but fixed order? )

但是对于字节序列,字节的顺序就是字节的顺序。 大尾数和小尾数是通过将较大项的位安排在字节数组中的方式定义的。对于字节序列,它们按存储顺序存储-不可能有其他顺序。这就像在问数字1、2、3、4是否与数字1、2、3、4有所不同吗?,当然不能。

But for a sequence of bytes, the order of the bytes is the order of the bytes. "Big endian" and "little endian" is defined by the way that the bits of a larger item are arranged in an array of bytes. For a sequence of bytes, they are stored in the order they are stored - there is no possibility of any other order. It's like asking "could the numbers 1, 2, 3, 4 be anything different than the numbers 1, 2, 3, 4? " and of course they can't.

这篇关于char数组在小端或大端系统中的顺序是否会有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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