#include iostream#include cstdint#include vector#include cstring#include arpainethint main uint16_t mapId = 1687228718; uint16_t tmpMapId = htonsmapId; stdvectoruint8_t resOut; uint8_t pt
该程序的功能是将一个16位无符号整数(mapId)进行网络字节序(Big-Endian)转换,然后将其转换为字节流,最后再将字节流转换回原始的mapId。
程序的输出为:
0x64 0x56 Restored mapId: 16872
首先,使用htons函数将mapId从主机字节序转换为网络字节序(Big-Endian),并将结果存储在tmpMapId中。
然后,将tmpMapId的字节表示形式存储在std::vector<uint8_t> resOut中,以便在网络上传输。
接下来,使用std::memcpy函数将resOut中的字节复制回restoredTmpMapId中,并使用ntohs函数将其从网络字节序转换回主机字节序,恢复原始的mapId。
最后,程序输出恢复的mapId。
原文地址: http://www.cveoy.top/t/topic/hmqB 著作权归作者所有。请勿转载和采集!