[C&C++]大小端字节序转换程序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/humanking7/article/details/81090308


原创文章,欢迎转载。转载请注明:转载自 祥的博客

原文链接:https://blog.csdn.net/humanking7/article/details/81090308


1.核心代码

//在pData为指针的unsigned char数据中
//将下标为startIndex开始,长度为length的这段数据进行大小端转换
//其原理就是数组元素的倒序
void EndianSwap(uint8 *pData, int startIndex, int length)
{
    int i,cnt,end,start;
    cnt = length / 2;
    start = startIndex;
    end  = startIndex + length - 1;
    uint8 tmp;
    for (i = 0; i < cnt; i++)
    {
        tmp            = pData[start+i];
        pData[start+i] = pData[end-i];
        pData[end-i]   = tmp;
    }
}

如果一个结构体的排列非常有规律(例如下面的示例,结构体的数据变量都是用4个字节uint32进行排列的),调用这个函数进行大小端转换就非常方便。

2.代码

2.1.使用示例

#include <iostream>
#include <iomanip>
using namespace std;

//signed
typedef signed char        int8;
typedef short              int16;
typedef int                int32;
typedef long long          int64;
//unsigned
typedef unsigned char      uint8;
typedef unsigned short     uint16;
typedef unsigned int       uint32;
typedef unsigned long long uint64;

#pragma pack(push)
#pragma pack(1)//单字节对齐
typedef struct{
    uint32 ID;
    uint32 Num;
    uint32 Type;
    uint32 lat;
    uint32 lng;
    uint32 alt;
    uint32 speed;
}Waypoint;//Payload_Data

#pragma pack(pop)




void EndianSwap(uint8 *pData, int startIndex, int length);



int main()
{

    Waypoint wp,wp_ori;
    int len = sizeof(Waypoint);
    cout << "size of Waypoint: " << len << endl;

    wp.ID    = 0x00000011;
    wp.Num   = 0x00002200;
    wp.Type  = 0xDD0CB0AA;
    wp.lat   = 0x00330000;
    wp.lng   = 0x44000000;
    wp.alt   = 0xABCD1234;
    wp.speed = 0x12345678;

    wp_ori = wp;


    int i = 0;
    uint8* pData = (uint8*)(&wp);
    for (i = 0; i < len; i += 4)
    {
        EndianSwap(pData,i,4);
    }


    cout << endl;
    cout << uppercase << hex << "改变字节序前: 0x" << setfill('0') << setw(8) << wp_ori.ID << endl;
    cout << uppercase << hex << "改变字节序后: 0x" <<setfill('0') << setw(8) << wp.ID <<endl;
    cout << endl;
    cout << uppercase << hex << "改变字节序前: 0x" << setfill('0') << setw(8) << wp_ori.Num << endl;
    cout << uppercase << hex << "改变字节序后: 0x" << setfill('0') << setw(8) << wp.Num << endl;
    cout << endl;
    cout << uppercase << hex << "改变字节序前: 0x" << setfill('0') << setw(8) << wp_ori.Type << endl;
    cout << uppercase << hex << "改变字节序后: 0x" << setfill('0') << setw(8) << wp.Type << endl;
    cout << endl;
    cout << uppercase << hex << "改变字节序前: 0x" << setfill('0') << setw(8) << wp_ori.lat << endl;
    cout << uppercase << hex << "改变字节序后: 0x" << setfill('0') << setw(8) << wp.lat << endl;
    cout << endl;
    cout << uppercase << hex << "改变字节序前: 0x" << setfill('0') << setw(8) << wp_ori.lng << endl;
    cout << uppercase << hex << "改变字节序后: 0x" << setfill('0') << setw(8) << wp.lng << endl;
    cout << endl;
    cout << uppercase << hex << "改变字节序前: 0x" << setfill('0') << setw(8) << wp_ori.alt << endl;
    cout << uppercase << hex << "改变字节序后: 0x" << setfill('0') << setw(8) << wp.alt << endl;
    cout << endl;
    cout << uppercase << hex << "改变字节序前: 0x" << setfill('0') << setw(8) << wp_ori.speed << endl;
    cout << uppercase << hex << "改变字节序后: 0x" << setfill('0') << setw(8) << wp.speed << endl;
    return 0;
}

void EndianSwap(uint8 *pData, int startIndex, int length)
{
    int i,cnt,end,start;
    cnt = length / 2;
    start = startIndex;
    end  = startIndex + length - 1;
    uint8 tmp;
    for (i = 0; i < cnt; i++)
    {
        tmp            = pData[start+i];
        pData[start+i] = pData[end-i];
        pData[end-i]   = tmp;
    }
}

2.2.效果

IMG20180717232024


赞赏码New

猜你喜欢

转载自blog.csdn.net/humanking7/article/details/81090308