2函数风格不同 看着不舒服 统一一下
#include <stdio.h>
#include <string.h>
typedef unsigned int uint32_t ;
typedef unsigned short uint16_t ;
typedef unsigned char uint8_t ;
uint16_t exchangeBytes(uint16_t value)
{
#if 1
uint16_t tmp_value = 0;
uint8_t *index_1, *index_2;
index_1 = (uint8_t *)&tmp_value;
index_2 = (uint8_t *)&value;
*index_1 = *(index_2+1);
*(index_1+1) = *index_2;
return tmp_value;
#else
return (((value & 0x00FF) << 8) | (((value & 0xFF00) >> 8) ));
#endif
}
uint32_t exchangeWord(uint32_t value)
{
#if 1
uint32_t tmp_value = 0;
uint8_t *index_1, *index_2;
index_1 = (uint8_t *)&tmp_value;
index_2 = (uint8_t *)&value;
*(index_1+0) = *(index_2+3);
*(index_1+1) = *(index_2+2);
*(index_1+2) = *(index_2+1);
*(index_1+3) = *(index_2+0);
return tmp_value;
#else
return ((value & 0x000000FF) << 24) |
((value & 0x0000FF00) << 8) |
((value & 0x00FF0000) >> 8) |
((value & 0xFF000000) >> 24) ;
#endif
}
int main(void)
{
uint16_t weight=0XABCD;
uint32_t weight2=0XABCD1234;
weight=exchangeBytes(weight);
printf("%04x",weight);
weight2=exchangeWord(weight2);
printf("%04x",weight2);
return 1;
}
/*万能打印*/
void G_print(void* A,uint8_t len,uint8_t c)
{
if(c)
for(uint8_t i=0;i<len;i++)
printf("%c ",*( (unsigned char*)A+i ) );
else
for(uint8_t i=0;i<len;i++)
printf("%02X ",*( (unsigned char*)A+i ) );
printf("\n");
}