我有一个具有这种结构的共享内存(参见代码片段 1),所有值都是十六进制值:
Position 00: 55; // memory overall, byte 00 to 03;
Position 01: 00;
Position 02: 00;
Position 03: 00;
Position 04: 47; // memory for header information, byte 04 to 07;
Position 05: 00
Position 06: 00;
Position 07: 00;
Position 08: 00; // version, byte 08, 09;
Position 09: 00;
Position 0A: 64; // rate of refreshing memory between processes
Position 0B: 00;
Position 0C: 00;
Position 0D: 00;
Position 0E: 00;
Position 0F: 4L;
...
正如您在评论中看到的那样,我知道哪个字节代表什么信息。无论如何,我将内存转换为结构(参见代码片段 2)。此结构中的属性当前是整数值。值 55 和 47 存储在前两个属性中。看起来,'00' 将被忽略,我无法逐字节读取整个内存。如何按字节读取内存?
代码片段 2:
struct Shm {
int memorySize; // size of memory space; min 4 bytes, Position 00 - 03; ie 55 is a hex value and means 84
int headerSize; // size of header space; min 4 bytes, Position 04 - 07; ie 47 (hex), so 71 (dec) same number as config-dialog
int byte3; // version number
int byte4; // refreshing interval in msec
...
此外,内存中有一些区域包含一些字符 - 如何将这些字节值转换为字符并创建它们的单词,目前我只能转换为 int 值(参见代码片段 3)
int main(void){
std::cout << "*** Start SharedMemory ***\n";
HANDLE hMapFile;
...
Shm * pBuf = (Shm *) MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
std::cout << " Debug memorySize " << ": " << dec << pBuf->memorySize << " (dec); " << hex << pBuf->memorySize << " (hex); " << &pBuf->memorySize << " (Adresse);\n"; // 84 Bytes
std::cout << " Debug headerSize " << ": " << dec << pBuf->headerSize << " (dec); << hex << pBuf->headerSize << " (hex);\n"; // 71 Bytes
std::cout << " Debug byte3 " << ": " << dec << pBuf->byte3 << " (dec); " << hex << pBuf->byte3 << " (hex);\n";
...