公司网站首页大图怎么做,怎么查询公司企业邮箱,网站图片上传不上去怎么办,站酷网络文章目录 大小端数据介绍大小端数据在内存中的样子C大小端数据转换QtAPI大小端转换 大小端数据介绍 大端#xff08;Big Endian#xff09;和小端#xff08;Little Endian#xff09;是一种描述计算机存储多字节数据的方式。 想象一下#xff0c;你有一串数字#xff0c… 文章目录 大小端数据介绍大小端数据在内存中的样子C大小端数据转换QtAPI大小端转换 大小端数据介绍 大端Big Endian和小端Little Endian是一种描述计算机存储多字节数据的方式。 想象一下你有一串数字例如1234这个数字需要用两个字节或更多来存储。那么问题是这个数字的哪个部分先存储在内存的起始位置低地址哪个部分先存储在内存的结束位置高地址 大端在大端字节序中像1234这样的数字的高位数字例如12会存储在内存的低地址处而低位数字例如34会存储在内存的高地址处。这就好比你在阅读数字时先读高位再读低位就像阅读英文文字一样从左到右。小端在小端字节序中相同的数字1234的低位数字例如34会存储在内存的低地址处而高位数字例如12会存储在内存的高地址处。这就好比你在阅读数字时先读低位再读高位就像阅读阿拉伯数字一样从右到左。
不同的计算机架构使用不同的字节序没有统一的标准。通常x86架构包括大多数个人电脑和服务器使用小端字节序而一些其他架构如PowerPC和SPARC使用大端字节序。因此当你在处理二进制数据或者与其他系统进行数据交互时需要注意字节序的差异以确保数据被正确解释和处理。这也是为什么在网络通信中会有网络字节序通常是大端和主机字节序取决于计算机架构之间的转换
大小端数据在内存中的样子 大端Big Endian和小端Little Endian是两种不同的内存存储格式它们定义了多字节数据如整数、浮点数等在内存中的排列方式。理解这两种格式对于编程和系统设计非常重要特别是在涉及到网络通信和跨平台数据交换时。 大端模式Big Endian
在大端模式下数据的大端即最高有效字节存储在内存的低地址端而数据的小端即最低有效字节存储在内存的高地址端。这意味着比如一个32位的整数0x12345678在内存中的存储顺序从低地址到高地址将是
0x12 0x34 0x56 0x78小端模式Little Endian
相反在小端模式下数据的小端即最低有效字节存储在内存的低地址端而数据的大端即最高有效字节存储在内存的高地址端。同样的32位整数0x12345678在内存中的存储顺序将是
0x78 0x56 0x34 0x12#mermaid-svg-SJNgCFFVT2itC4wO {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-SJNgCFFVT2itC4wO .error-icon{fill:#552222;}#mermaid-svg-SJNgCFFVT2itC4wO .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-SJNgCFFVT2itC4wO .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-SJNgCFFVT2itC4wO .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-SJNgCFFVT2itC4wO .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-SJNgCFFVT2itC4wO .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-SJNgCFFVT2itC4wO .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-SJNgCFFVT2itC4wO .marker{fill:#333333;stroke:#333333;}#mermaid-svg-SJNgCFFVT2itC4wO .marker.cross{stroke:#333333;}#mermaid-svg-SJNgCFFVT2itC4wO svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-SJNgCFFVT2itC4wO .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-SJNgCFFVT2itC4wO .cluster-label text{fill:#333;}#mermaid-svg-SJNgCFFVT2itC4wO .cluster-label span{color:#333;}#mermaid-svg-SJNgCFFVT2itC4wO .label text,#mermaid-svg-SJNgCFFVT2itC4wO span{fill:#333;color:#333;}#mermaid-svg-SJNgCFFVT2itC4wO .node rect,#mermaid-svg-SJNgCFFVT2itC4wO .node circle,#mermaid-svg-SJNgCFFVT2itC4wO .node ellipse,#mermaid-svg-SJNgCFFVT2itC4wO .node polygon,#mermaid-svg-SJNgCFFVT2itC4wO .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-SJNgCFFVT2itC4wO .node .label{text-align:center;}#mermaid-svg-SJNgCFFVT2itC4wO .node.clickable{cursor:pointer;}#mermaid-svg-SJNgCFFVT2itC4wO .arrowheadPath{fill:#333333;}#mermaid-svg-SJNgCFFVT2itC4wO .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-SJNgCFFVT2itC4wO .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-SJNgCFFVT2itC4wO .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-SJNgCFFVT2itC4wO .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-SJNgCFFVT2itC4wO .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-SJNgCFFVT2itC4wO .cluster text{fill:#333;}#mermaid-svg-SJNgCFFVT2itC4wO .cluster span{color:#333;}#mermaid-svg-SJNgCFFVT2itC4wO div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-SJNgCFFVT2itC4wO :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Little Endian Memory Layout Big Endian Memory Layout 0x00: 0x78 0x01: 0x56 0x02: 0x34 0x03: 0x12 0x00: 0x12 0x01: 0x34 0x02: 0x56 0x03: 0x78 在这个图表中我们展示了一个32位整数0x12345678在大端和小端模式下的内存布局。每个方框代表一个字节的内存位置箭头表示地址的增加方向。从图表中可以清晰地看出大端模式将最高有效字节放在最低的内存地址处而小端模式则相反将最低有效字节放在最低的内存地址处。
通过这样的图表可以直观地理解大端和小端模式的不同以及它们在内存中是如何存储数据的。
C大小端数据转换
memcpy大小端转换
// 大端转小端
uint16_t bigEndianValue 0x1234;
uint16_t littleEndianValue;
memcpy(littleEndianValue, bigEndianValue, sizeof(uint16_t));// 小端转大端
uint16_t littleEndianValue 0x3412;
uint16_t bigEndianValue;
memcpy(bigEndianValue, littleEndianValue, sizeof(uint16_t));宏函数大小端转换
uint16_t bigEndianValue 0x1234;
uint16_t littleEndianValue BIG_ENDIAN_TO_LITTLE_ENDIAN_16(bigEndianValue);uint32_t anotherBigEndianValue 0x12345678;
uint32_t anotherLittleEndianValue BIG_ENDIAN_TO_LITTLE_ENDIAN_32(anotherBigEndianValue);
boost库
#include boost/endian/conversion.hppuint16_t bigEndianValue 0x1234;
uint16_t littleEndianValue boost::endian::endian_reverse(bigEndianValue);
C20判断大小端
#include iostream
#include cstdintint main() {if (std::endian::native std::endian::big) {std::cout This system uses big endian. std::endl;} else if (std::endian::native std::endian::little) {std::cout This system uses little endian. std::endl;} else {std::cout Unknown endian. std::endl;}return 0;
}结构体的大小端转换
#include iostream
#include cstdint
#include cstring//1字节对齐
#pragma pack(push, 1)
struct MyStruct {uint16_t a;uint32_t b;uint16_t c;
};
#pragma pack(pop)MyStruct bigToLittleEndian(const MyStruct bigEndianValue) {MyStruct littleEndianValue;std::memcpy(littleEndianValue, bigEndianValue, sizeof(MyStruct));return littleEndianValue;
}MyStruct littleToBigEndian(const MyStruct littleEndianValue) {return bigToLittleEndian(littleEndianValue);
}int main() {MyStruct bigEndianValue {0x1234, 0x56789ABC, 0x5678};MyStruct littleEndianValue bigToLittleEndian(bigEndianValue);std::cout Big Endian Value: std::hex bigEndianValue.a bigEndianValue.b bigEndianValue.c std::endl;std::cout Little Endian Value: std::hex littleEndianValue.a littleEndianValue.b littleEndianValue.c std::endl;return 0;
}QtAPI大小端转换
QByteArray
#include QByteArray
#include QtEndian// 大端转小端
QByteArray bigToLittleEndian(const QByteArray bigEndianData) {QByteArray littleEndianData(bigEndianData);for (int i 0; i littleEndianData.size(); i) {littleEndianData[i] bigEndianData.at(littleEndianData.size() - 1 - i);}return littleEndianData;
}// 小端转大端
QByteArray littleToBigEndian(const QByteArray littleEndianData) {QByteArray bigEndianData(littleEndianData);for (int i 0; i bigEndianData.size(); i) {bigEndianData[i] littleEndianData.at(bigEndianData.size() - 1 - i);}return bigEndianData;
}int main() {uint16_t bigEndianValue 0x1234;QByteArray bigEndianData(reinterpret_castconst char*(bigEndianValue), sizeof(uint16_t));QByteArray littleEndianData bigToLittleEndian(bigEndianData);QByteArray backToBigEndian littleToBigEndian(littleEndianData);// 输出结果qDebug() Big Endian Data: bigEndianData.toHex();qDebug() Little Endian Data: littleEndianData.toHex();qDebug() Back to Big Endian Data: backToBigEndian.toHex();return 0;
}
QDataStream
QDataStream stream(byteArray, QIODevice::ReadWrite);
stream.setByteOrder(QDataStream::LittleEndian); // 设置为小端
quint16 value;
stream value; // 从小端数据中读取16位无符号整数
QByteArray
quint16 bigEndianValue 0x1234;
QByteArray byteArray QByteArray(reinterpret_castconst char*(bigEndianValue), sizeof(quint16));
quint16 littleEndianValue qFromBigEndianquint16(byteArray.constData());
判断大小端
if (QSysInfo::ByteOrder QSysInfo::LittleEndian) {// 当前系统是小端
} else {// 当前系统是大端
}qFromLittleEndian、qToLittleEndian
quint16 littleEndianValue 0x1234;
quint16 nativeValue qFromLittleEndian(littleEndianValue); // 转换为本机字节顺序quint16 nativeValue 0x1234;
quint16 littleEndianValue qToLittleEndian(nativeValue); // 转换为小端表示