如何将unsigned char值从little转换为big endian? [英] How to convert unsigned char value from little to big endian?

查看:114
本文介绍了如何将unsigned char值从little转换为big endian?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

unsigned char数组为

The unsigned char array is given as

unsigned char dmp1[] =
"\x54\x49\x4C\xA6\x3E\xBA\x03\x37\xE4\xE2\x40\x23\xFC\xD6\x9A\x5A"
"\xEB\x07\xDD\xDC\x01\x83\xA4\xD0\xAC\x9B\x54\xB0\x51\xF2\xB1\x3E"
"\xD9\x49\x09\x75\xEA\xB7\x74\x14\xFF\x59\xC1\xF7\x69\x2E\x9A\x2E"
"\x20\x2B\x38\xFC\x91\x0A\x47\x41\x74\xAD\xC9\x3C\x1F\x67\xC9\x81";





unsigned char = 1字节,如果我们互换它的小端和大端格式相同,因为我们只能转换多字节值的字节顺序

示例:将多字节值视为0x34 0x35 0x46 0x45。如果是小端。

当我们转换它时o大端,它变成,0x45 0x46 0x35 0x34。



如果是这样,上面的dmp1值应该从最后一个到第一个交换,将它转换为大端风格。



任何人都可以帮忙解释一下这是怎么回事?



我有什么尝试过:



我尝试将dmp1的字节值从最后一个换成第一个,但没有得到我预期的输出。



unsigned char = 1 byte, if we interchage it is same in little endian and big endian formats because we can convert endianness of only multibyte values
Example: Consider the multibyte value as 0x34 0x35 0x46 0x45 .If it is in little endian.
When we convert it to big endian it becomes, 0x45 0x46 0x35 0x34.

If it so, the dmp1 value above should be interchange from the last to first to convert it in to big endian style.

Can any one help explain how it is so?

What I have tried:

I have tried interchanging byte values of dmp1 from last to first but didnt get the output what i expected.

推荐答案

好的,所以你可能没有得到你期望的答案,因为你必须参考一些符号大小进行字节交换。正如谢尔盖上面解释的那样,符号大小可以是16位,32位等等。



32位是一个常见的大小(你的例子使用那个大小)..那么我们就去吧...



所以你应该做的是(伪代码):

Ok, so you probably didn't get the answer you expected because you have to do byte-swapping in reference to some symbol size. As Sergey explained above, the symbol size can be 16 bits, 32 bits, or whatever.

32 bits is a common size (your example uses that size)... so let's go with that...

So what you should do is (in pseudo-code):
offset=0;
while(offset<dmp1_size){>
  buff[symbol_size] = *dmp1+offset;
  //do your swap here for 32 bits (4 bytes) on buff
  offset+=symbol_size;
}





这有意义吗? ...你必须一次交换一个符号,而不是一次完成整个缓冲区。



Does that make sense? ...you have to do your swapping one symbol at a time, not do the whole buffer at once.


这篇关于如何将unsigned char值从little转换为big endian?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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