高低字节转换例子

这是一个关于如何进行高低字节转换的C++代码示例。通过位运算和内存操作,将整型和结构体数据的字节顺序进行转换,涉及缓冲区操作和浮点数表示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// ConvertData.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
//#define cdr_int32_to(from) \
//    ( (((from) & (0xfful << 24)) >> 24) \
//    | (((from) & (0xfful << 16)) >> 8) \
//    | (((from) & (0xfful << 8)) << 8) \
//    | (((from) & 0xfful) << 24) )

#define cdr_int32_to(from) \
    ( (((from) & (0xff << 24)) >> 24) \
    | (((from) & (0xff << 16)) >> 8) \
    | (((from) & (0xff << 8)) << 8) \
    | (((from) & 0xff) << 24) )
struct MysDI{
    unsigned int mydi1 :1;
    unsigned int mydi2 :1;
};
struct struct_data{
    int a;
    int b;
    MysDI myDI;
};

int _tmain(int argc, _TCHAR* argv[])
{    
    //system("color 2");
    int data = 0x12345678;
    //dataConvert(data);
    //data = ( (((data) & (0xfful << 24)) >> 24) | (((data) & (0xfful << 16)) >> 8)     | (((data) & (0xfful << 8)) << 8)     | (((data) & 0xfful) << 24) );
    data = ( (((data) & (0xff << 24)) >> 24) | (((data) & (0xff << 16)) >> 8)     | (((data) & (0xff << 8)) << 8) | (((data) & 0xff) << 24) );

    cdr_int32_to(data);
    char buffer[20];
    char buffer1[20];
    //char *pBuff = (char*)&data;
    memset(buffer,0,sizeof(buffer));
    memset(buffer1,0,sizeof(buffer1));
    buffer[3] =  *(char*)&data;
    buffer[2] =  *((char*)&data+1);
    buffer[1] =  *((char*)&data+2);
    buffer[0] =  *((char*)&data+3);

    buffer1[0] =  *(char*)&data;
    buffer1[1] =  *((char*)&data+1);
    buffer1[2] =  *((char*)&data+2);
    buffer1[3] =  *((char*)&data+3);

    //printf("data source ----> %f \n", data);
    //printf("dataConvert ----> %f \n", /*data*/*(float*)buffer);
    //printf("dataConvert1 ----> %f \n", /*data*/*(float*)buffer1);

    //================test struct======================
    struct_data Mystruct,fromPLC;
    Mystruct.a = Mystruct.b = 0x12345678;
    Mystruct.myDI.mydi1 =  1;
    Mystruct.myDI.mydi2 = 0;

    char *p = (char*)&fromPLC;
    printf("data source ----> %x, %x \n", Mystruct.a,Mystruct.b, 
        Mystruct.myDI.mydi1,Mystruct.myDI.mydi2);

    char struct_buffer[20],convert_buff[20];
    memset(struct_buffer,0,sizeof(struct_buffer));
    memset(convert_buff,0,sizeof(convert_buff));

    memcpy(struct_buffer,&Mystruct,sizeof(struct_data));
    memcpy(convert_buff,&Mystruct,sizeof(struct_data));
    //for (int i = 0; i < 2; i ++)
    //{
    //    convert_buff[i*4 + 0] = struct_buffer[i*4 + 3]; 
    //    convert_buff[i*4 + 1] = struct_buffer[i*4 + 2]; 
    //    convert_buff[i*4 + 2] = struct_buffer[i*4 + 1]; 
    //    convert_buff[i*4 + 3] = struct_buffer[i*4 + 0]; 
    //}

    //memcpy(&fromPLC,convert_buff,sizeof(struct_data));

    for (int i = 0; i < 2; i ++)
    {
        p[i*4 + 0] = struct_buffer[i*4 + 3]; 
        p[i*4 + 1] = struct_buffer[i*4 + 2]; 
        p[i*4 + 2] = struct_buffer[i*4 + 1]; 
        p[i*4 + 3] = struct_buffer[i*4 + 0]; 
    }
    
    printf("data convert ----> %x, %x \n", fromPLC.a,fromPLC.b);

    printf("data convert ----> %d, %d \n", fromPLC.myDI.mydi1,fromPLC.myDI.mydi2);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值