【发布时间】:2013-11-28 00:14:06
【问题描述】:
我正在使用 MapViewOfFile() 和 SharedMemory。我能够逐字节读取内存内容!现在我想知道,如何将新的十六进制值设置为特定字节?由于我的代码,我希望在我的第二个 console.log 中,十六进制值 0xffc8 在单元格 83 中。不幸的是,情况并非如此。
// main method
FILE * pBuf = (FILE*) MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
...
int d;
BYTE dbgByte;
for(d = 0; d < 86; d++){
dbgByte = *((PBYTE) pBuf + (d));
printf("DEBUG byte %i hexvalue %hhx \n", d, (char) dbgByte);
printf("DEBUG byte %i int %i \n", d, (int) dbgByte);
}
// DEBUG - END
for(d = 0; d < 86; d++){
if (d == 83){ // 0xffc8 = 200
BYTE writeByte1;
writeByte1 = *((PBYTE) pBuf + (d));
writeByte1 = 0xffc8;
}
}
// DEBUG 2 - START
for(d = 0; d < 86; d++){
dbgByte = *((PBYTE) pBuf + (d));
printf("DEBUG byte %i hexvalue %hhx \n", d, (char) dbgByte);
printf("DEBUG byte %i int %i \n", d, (int) dbgByte);
}
// DEBUG - END
...
更新:尝试了比尔的建议 - 不幸的是,这也不起作用:
if (d == 84){ // 0x42 = 66
*((PBYTE) pBuf + (d)) = 0x42;
}
UPDATE-2:尝试了 Oblivious 船长的建议 - 不幸的是,写作程序不起作用。我在 debug-3 日志记录语句中看不到十六进制值 42。
for(d = 0; d < 86; d++){
byte = pBuf[d];
printf("DEBUG-1 ");
printf("hex: %hhx; ", byte);
printf("char: %c; ", (char) byte);
printf("dec: %i; ", (int) byte);
printf(" byte %i; ", d);
printf("\n");
if (d == 84){ // 0x42 = 66
pBuf[d] = 0x42;
printf("DEBUG-3 ");
printf("hex: %hhx; ", byte);
printf("char: %c; ", (char) byte);
printf("dec: %i; ", (int) byte);
printf(" byte %i; ", d);
printf("\n");
}
}
【问题讨论】:
-
你打算如何将
0xffc8放在一个字节中?
标签: c++ c shared-memory