我在 Android 上使用 SDL,尝试加载此文件:
00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00
01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01
02 00 11 04 04 04 04 04 04 04 04 04 04 05 01 02
00 01 10 03 03 03 03 03 03 03 03 03 03 06 02 00
01 02 10 03 08 08 08 08 08 08 08 03 03 06 00 01
02 00 10 06 00 01 02 00 01 02 00 10 03 06 01 02
00 01 10 06 01 11 05 01 02 00 01 10 03 06 02 00
01 02 10 06 02 09 07 02 00 01 02 10 03 06 00 01
02 00 10 06 00 01 02 00 01 02 00 10 03 06 01 02
00 01 10 03 04 04 04 05 02 00 01 09 08 07 02 00
01 02 09 08 08 08 08 07 00 01 02 00 01 02 00 01
02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02
像这样进入 std::istringstream:
int blocks;
char buf[256];
SDL_RWops *rw=SDL_RWFromFile("files/test.map","rb");
blocks=SDL_RWread(rw,buf,16,256/16);
SDL_RWclose(rw);
SDL_Log("Read %d 16-byte blocks",blocks);
SDL_Log("%s",buf);
std::string stringvalues = buf;
std::istringstream map (stringvalues);
当我尝试使用 SDL_LOG("%s") 查看 buf 的内容时,我没有看到上面我期望的内容:
09-22 16:24:32.654 8651 8668 I SDL/APP : 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00
09-22 16:24:32.654 8651 8668 I SDL/APP : 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01
09-22 16:24:32.654 8651 8668 I SDL/APP : 02 00 11 04 04 04 04 04 04 04 04 04 04 05 01 02
09-22 16:24:32.654 8651 8668 I SDL/APP : 00 01 10 03 03 03 03 03 03 03 03 03 03 06 02 00
09-22 16:24:32.654 8651 8668 I SDL/APP : 01 02 10 03 08 08 08 08 08 08 08 03 03 06 00 01
09-22 16:24:32.654 8651 8668 I SDL/APP : 02 00 �wNL���5�
有没有办法让我打印每个 char 元素的十六进制值,以便我可以更好地调试它?或者大家有更好的想法也欢迎提出,谢谢
最佳答案
我猜 blocks
是正确的 16
而您只是想要一种方法来检查缓冲区中的字节。
下面代码中的 GetBufDump
函数是一个调试实用函数,它使用缓冲区中字节的格式化表示填充 std::string
(有点像DOS 调试会做到这一点)。
#include <iostream>
#include <string>
std::string GetBufDump(const void* buf, std::size_t size, std::size_t line_len=16) {
static const char digits[] = "0123456789abcdef";
const unsigned char *p = static_cast<const unsigned char*>(buf);
const unsigned char *end = p + size;
std::string dump;
std::string ascii;
while(p != end) {
const unsigned char byte = *(p++);
dump += digits[byte/16];
dump += digits[byte%16];
dump += ' ';
ascii += byte < ' ' || byte >= 127 ? '.' : static_cast<char>(byte);
if(ascii.length() == line_len) {
dump += ' ';
dump += ascii;
dump += '\n';
ascii.clear();
}
}
if(!ascii.empty()) {
std::size_t padding = line_len - ascii.length();
dump += std::string(padding * 3, ' ');
dump += ' ';
dump += ascii;
dump += '\n';
}
return dump;
}
int main() {
const char test_bytes[] =
"Now is the time for all good men to "
"jump over the lazy dogs\0\xfe\x01***<3\n";
std::string dump = GetBufDump(test_bytes, sizeof(test_bytes));
std::cout << dump << '\n';
}
这个输出:
4e 6f 77 20 69 73 20 74 68 65 20 74 69 6d 65 20 Now is the time
66 6f 72 20 61 6c 6c 20 67 6f 6f 64 20 6d 65 6e for all good men
20 74 6f 20 6a 75 6d 70 20 6f 76 65 72 20 74 68 to jump over th
65 20 6c 61 7a 79 20 64 6f 67 73 00 fe 01 2a 2a e lazy dogs...**
2a 3c 33 0a 00 *<3..
如果 SDL_RWread
返回的计数小于预期的计数,您可能需要多次调用它,直到读取了所有预期的数据或者它返回 0
,表明它遇到了输入流结束。
关于android - 在 Android 上使用 SDL_Log 打印 char 的十六进制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39648457/