我在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元素打印十六进制值,以便我可以更好地调试它?或者,如果有人有更好的想法,请随时提出建议,谢谢
答案 0 :(得分:1)
我猜测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
,表明它遇到了输入流的结束