标签:方法 存在 ack man payload signed 对齐 clu 分享
大端模式: 数据的高字节存在低地址 数据的低字节存在高地址
小端模式: 数据的高字节存在高地址 数据的低字节存在低地址

如图,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;
}
}
运行结果如下:

标签:方法 存在 ack man payload signed 对齐 clu 分享
原文地址:https://www.cnblogs.com/wuyepeng/p/9833273.html