1. 程式人生 > >[C&C++]大小端字節序轉換程序

[C&C++]大小端字節序轉換程序

分享圖片 ostream 利用 div swap man image set strong

大端模式: 數據的高字節存在低地址 數據的低字節存在高地址

小端模式: 數據的高字節存在高地址 數據的低字節存在低地址

技術分享圖片

如圖,i為int類型占4個字節,但只有1個字節的值為1,另外3個字節值為0;取出低地址上的值,當其為1時則為小端模式,為0時為大端模式。

//大小端模式的判斷
//方法一:利用聯合體所有成員的起始位置一致,
//對聯合體中的int類型賦值,然後判斷聯合體中char類型的值的大小
#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;
    }
}

  運行結果如下:

技術分享圖片

[C&C++]大小端字節序轉換程序