统一机智云的2个函数

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");
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值