将大端转换为小端 [英] converting a big endian to little endian
问题描述
大家好,
我需要将大端整数转换为小端整数。
(我的实现中整数大小为4个字节) )。我想出了以下代码
。我需要你对此发表评论。请建议任何可以完成的
改进。
#include< stdio.h>
int main(void)
{
int big = 0x12345678;
int little;
char * big_ptr =(char *)& ;大;
char * little_ptr =(char *)& little;
little_ptr [0] = big_ptr [3];
little_ptr [1] = big_ptr [2];
little_ptr [2] = big_ptr [1];
little_ptr [3] = big_ptr [0];
printf(大= 0x%x小= 0x%x \ n,大,小);
}
Hi guys,
I need to convert a big endian integer to little endian integer.
(the integer is 4 bytes in size on my implementation). I came up with
the following code. I need your comments on this. Please suggest any
improvements that can be done.
#include <stdio.h>
int main(void)
{
int big = 0x12345678;
int little;
char *big_ptr = (char *)&big;
char *little_ptr = (char *)&little;
little_ptr[0] = big_ptr[3];
little_ptr[1] = big_ptr[2];
little_ptr[2] = big_ptr[1];
little_ptr[3] = big_ptr[0];
printf("big = 0x%x little = 0x%x\n",big,little);
}
推荐答案
您好,
我需要将一个大端整数转换为小端整数。
(我的实现中整数大小为4个字节)。我想出了以下代码
。我需要你对此发表评论。请建议任何可以完成的
改进。
I need to convert a big endian integer to little endian integer.
(the integer is 4 bytes in size on my implementation). I came up with
the following code. I need your comments on this. Please suggest any
improvements that can be done.
您可以使用''shift''与''和''运算符相结合来获得相同的
结果:
/ *
*转换big -little endian和conversly
*此函数假定sizeof(unsigned int)== 4
* /
unsigned int
endian_swap(unsigned int x)
{
return < br $> b $ b(x>> 24)|
((x>> 8)& 0x0000ff00)|
((x<< 8)& 0x00ff0000)|
(x << 24);
}
干杯,
Loic。
You can use ''shift'' combined with ''and'' operator to get the same
result:
/*
* convert big -little endian and conversly
* this function assumes that sizeof(unsigned int) == 4
*/
unsigned int
endian_swap(unsigned int x)
{
return
(x>>24) |
((x>>8) & 0x0000ff00) |
((x<<8) & 0x00ff0000) |
(x<<24);
}
Cheers,
Loic.
< ju ********** @ yahoo.co.inwrote in message
news:11 ********************** @ 80g2000cwy.googlegro ups.com ...
<ju**********@yahoo.co.inwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...
大家好,
我需要将大端整数转换为小端整数。
(整数大小为4字节)我的实施)。我想出了以下代码
。我需要你对此发表评论。请建议任何可以完成的
改进。
#include< stdio.h>
int main(void)
{
int big = 0x12345678;
int little;
char * big_ptr =(char *)& ;大;
char * little_ptr =(char *)& little;
little_ptr [0] = big_ptr [3];
little_ptr [1] = big_ptr [2];
little_ptr [2] = big_ptr [1];
little_ptr [3] = big_ptr [0];
printf(大= 0x%x小= 0x%x \ n,大,小);
}
Hi guys,
I need to convert a big endian integer to little endian integer.
(the integer is 4 bytes in size on my implementation). I came up with
the following code. I need your comments on this. Please suggest any
improvements that can be done.
#include <stdio.h>
int main(void)
{
int big = 0x12345678;
int little;
char *big_ptr = (char *)&big;
char *little_ptr = (char *)&little;
little_ptr[0] = big_ptr[3];
little_ptr[1] = big_ptr[2];
little_ptr[2] = big_ptr[1];
little_ptr[3] = big_ptr[0];
printf("big = 0x%x little = 0x%x\n",big,little);
}
上面的代码应该可以正常工作。然而,更传统的做法是使用工会,即
工会
{
int i;
char c [4];
} pickapart;
pickapart.i = input_arg;
output0 = pickapart.c [0];
但是,如果你能找到一种只涉及转移的方法,并且如果要求
检查汇编语言表明编译器做得很好
(可能没有移位),这将是首选的方法,即
output0 = input_arg& ; 0xFF;
output1 =(input_arg>> 8)& 0xFF;
等
那么这将是首选方法。如果int大小<32,那么你所引用的方法可以导致内存寻址问题,并且引用的方法我不会为其他工作整数的大小。应该是一个更便携和一般的方法
。
The code above should work fine. However, the more traditional approach is
to use a union, i.e.
union
{
int i;
char c[4];
} pickapart;
pickapart.i = input_arg;
output0 = pickapart.c[0];
However, if you can find an approach involving shifting only, and if an
examination of the assembly-language shows that the compiler does it well
(probably without shifting), that would be the preferred approach, i.e.
output0 = input_arg & 0xFF;
output1 = (input_arg >>8) & 0xFF;
etc.
then that would be the preferred approach. The approach you cited can lead
to memory addressing problems if the int size is <32, and the approach I
cited with the union won''t work for other sizes of integers. There should
be a more portable and general approach.
这里新增了,如果我没有遵循任何规则而道歉,
添加代码片段等。请告诉我。
Hi,
New here, so apologies if I have not followed any rules while
adding code snippets etc. Do let me know.
endian_swap(unsigned int x)
{
返回
(x>> 24)|
((x>> 8) & 0x0000ff00)|
((x << 8)& 0x00ff0000)|
(x << 24);
}
干杯,
Loic。
endian_swap(unsigned int x)
{
return
(x>>24) |
((x>>8) & 0x0000ff00) |
((x<<8) & 0x00ff0000) |
(x<<24);
}
Cheers,
Loic.
如果int是32位,那么以下代码对于无符号
int'有多好?
int main(无效)
{
unsigned int x = 0xffaa2211;
unsigned int z = 0;
z =((x << 16)|(x> 16));
返回0;
}
If int is 32 bits, then how good is the following code for unsigned
int''s?
int main(void)
{
unsigned int x = 0xffaa2211;
unsigned int z = 0;
z = ( (x << 16) | ( x >16) );
return 0;
}
这篇关于将大端转换为小端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!