c中的字节顺序反转
reversing byte order in c
我在 cs:app 数据实验室中解决了 'reverseBytes' 问题。
我必须编写 returns 颠倒字节顺序的代码。
示例:输入=0x123456,returns=0x563412
当我使用我的代码时,它不能得分..
int reverseBytes(int x) {
int mask=0xff;
int byte1=x>>24;
int byte2=(x>>16)&mask;
int byte3=(x>>8)&mask;
int byte4=x&mask;
int result=(byte4<<24)|(byte3<<16)|(byte2<<8)|(byte1);
return result;
}
但是,当我使用别人的代码时,它需要一个分数。
int reverseBytes(int x) {
int t2=~(0xff<<24);
int s1=(0xff<<16)+0xff;
int s2=0xff<<8;
int s3=(s2<<16)+s2;
int temp=(x&s1)<<8|((x&s3)>>8&t2);
int q1=(0xff<<8)+0xff;
int q2=q1<<16;
int temp2=(temp&q1)<<16|((temp&q2)>>16&(~q2));
return temp2;
}
我不知道为什么我的代码不能工作..
我测试了我的代码和其他人的代码。
但我找不到我的代码结果和另一个代码结果之间的区别。
请帮助我..
简单的右移以获得所需的字节即可:
#include <stdio.h>
#include <stdint.h>
uint32_t reverse_bytes(uint32_t bytes)
{
uint32_t aux = 0;
uint8_t byte;
int i;
for(i = 0; i < 32; i+=8)
{
byte = (bytes >> i) & 0xff;
aux |= byte << (32 - 8 - i);
}
return aux;
}
Test:
int main(void) {
uint32_t input = 0x123456;
printf("input: 0x%08x\n", input);
input = reverse_bytes(input);
printf("input: 0x%08x\n", input);
return 0;
}
打印:
输入:0x00123456
输入:0x56341200
移位运算符保留签名。尝试将 "int byte1=x>>24;" 更改为“ "int byte1=x>>24&mask;"。
或者只是切换到使用无符号整数。
以下函数可以交换任意长度的字节串。 swapbytes()
交换 inp
内的 inp
参数中包含的所有字节,从而修改 inp
的内容。 swapbytesout()
交换 out
参数中 inp
参数的内容,然后它不会修改 inp
.
的内容
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
void * swapbytes(void *inp, size_t len);
void * swapbytesout(void * inp, void *out, size_t len);
/* This function swaps and modifies the content of inp
*
* Returns: a pointer to the output value (inp) */
void * swapbytes(void *inp, size_t len)
{
unsigned int i;
unsigned char *in=(unsigned char *)inp,tmp;
for(i=0;i<len/2;i++) {
tmp=*(in+i);
*(in+i)=*(in+len-i-1);
*(in+len-i-1)=tmp;
}
return inp;
}
/* This function doesn't swap and doesn't modify the content
* of inp, the output is computed on out.
*
* Returns: a pointer to the output value (out) */
void * swapbytesout(void *inp, void *out, size_t len)
{
unsigned int i;
unsigned char *o=(unsigned char *)out;
unsigned char *in=(unsigned char *)inp;
for(i=0;i<len;i++) {
*(o+len-i-1)=*(in+i);
}
return out;
}
int main(void)
{
uint32_t a0,a1;
uint64_t b0,b1;
a0=0x12345678;
printf("%08X\n",*(uint32_t *)swapbytesout(&a0,&a1,sizeof(a0)));
printf("%08X %08X\n",a0,a1);
printf("%08X\n",*(uint32_t *)swapbytes(&a0,sizeof(a0)));
printf("%08X\n",a0);
puts("");
b0=0x123456789ABCDEF0ULL;
printf("%016"PRIX64"\n",*(uint64_t *)swapbytesout(&b0,&b1,sizeof(b0)));
printf("%016"PRIX64" %016"PRIX64"\n",b0,b1);
printf("%016"PRIX64"\n",*(uint64_t *)swapbytes(&b0,sizeof(b0)));
printf("%016"PRIX64"\n",b0);
return 0;
}
使用这个:
#define reverse_bytes_32(num) ( ((num & 0xFF000000) >> 24) | ((num & 0x00FF0000) >> 8) | ((num & 0x0000FF00) << 8) | ((num & 0x000000FF) << 24) )
我在 cs:app 数据实验室中解决了 'reverseBytes' 问题。
我必须编写 returns 颠倒字节顺序的代码。
示例:输入=0x123456,returns=0x563412
当我使用我的代码时,它不能得分..
int reverseBytes(int x) {
int mask=0xff;
int byte1=x>>24;
int byte2=(x>>16)&mask;
int byte3=(x>>8)&mask;
int byte4=x&mask;
int result=(byte4<<24)|(byte3<<16)|(byte2<<8)|(byte1);
return result;
}
但是,当我使用别人的代码时,它需要一个分数。
int reverseBytes(int x) {
int t2=~(0xff<<24);
int s1=(0xff<<16)+0xff;
int s2=0xff<<8;
int s3=(s2<<16)+s2;
int temp=(x&s1)<<8|((x&s3)>>8&t2);
int q1=(0xff<<8)+0xff;
int q2=q1<<16;
int temp2=(temp&q1)<<16|((temp&q2)>>16&(~q2));
return temp2;
}
我不知道为什么我的代码不能工作.. 我测试了我的代码和其他人的代码。 但我找不到我的代码结果和另一个代码结果之间的区别。 请帮助我..
简单的右移以获得所需的字节即可:
#include <stdio.h>
#include <stdint.h>
uint32_t reverse_bytes(uint32_t bytes)
{
uint32_t aux = 0;
uint8_t byte;
int i;
for(i = 0; i < 32; i+=8)
{
byte = (bytes >> i) & 0xff;
aux |= byte << (32 - 8 - i);
}
return aux;
}
Test:
int main(void) {
uint32_t input = 0x123456;
printf("input: 0x%08x\n", input);
input = reverse_bytes(input);
printf("input: 0x%08x\n", input);
return 0;
}
打印:
输入:0x00123456
输入:0x56341200
移位运算符保留签名。尝试将 "int byte1=x>>24;" 更改为“ "int byte1=x>>24&mask;"。 或者只是切换到使用无符号整数。
以下函数可以交换任意长度的字节串。 swapbytes()
交换 inp
内的 inp
参数中包含的所有字节,从而修改 inp
的内容。 swapbytesout()
交换 out
参数中 inp
参数的内容,然后它不会修改 inp
.
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
void * swapbytes(void *inp, size_t len);
void * swapbytesout(void * inp, void *out, size_t len);
/* This function swaps and modifies the content of inp
*
* Returns: a pointer to the output value (inp) */
void * swapbytes(void *inp, size_t len)
{
unsigned int i;
unsigned char *in=(unsigned char *)inp,tmp;
for(i=0;i<len/2;i++) {
tmp=*(in+i);
*(in+i)=*(in+len-i-1);
*(in+len-i-1)=tmp;
}
return inp;
}
/* This function doesn't swap and doesn't modify the content
* of inp, the output is computed on out.
*
* Returns: a pointer to the output value (out) */
void * swapbytesout(void *inp, void *out, size_t len)
{
unsigned int i;
unsigned char *o=(unsigned char *)out;
unsigned char *in=(unsigned char *)inp;
for(i=0;i<len;i++) {
*(o+len-i-1)=*(in+i);
}
return out;
}
int main(void)
{
uint32_t a0,a1;
uint64_t b0,b1;
a0=0x12345678;
printf("%08X\n",*(uint32_t *)swapbytesout(&a0,&a1,sizeof(a0)));
printf("%08X %08X\n",a0,a1);
printf("%08X\n",*(uint32_t *)swapbytes(&a0,sizeof(a0)));
printf("%08X\n",a0);
puts("");
b0=0x123456789ABCDEF0ULL;
printf("%016"PRIX64"\n",*(uint64_t *)swapbytesout(&b0,&b1,sizeof(b0)));
printf("%016"PRIX64" %016"PRIX64"\n",b0,b1);
printf("%016"PRIX64"\n",*(uint64_t *)swapbytes(&b0,sizeof(b0)));
printf("%016"PRIX64"\n",b0);
return 0;
}
使用这个:
#define reverse_bytes_32(num) ( ((num & 0xFF000000) >> 24) | ((num & 0x00FF0000) >> 8) | ((num & 0x0000FF00) << 8) | ((num & 0x000000FF) << 24) )