2016-09-22 160 views
1

我使用SDL在Android上,试图加载该文件:十六进制值在Android

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); 

当我尝试查看BUF的使用SDL_LOG(“%S”)的内容,我没有看到什么我期待上面:

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� 

有我给首席方式t每个char元素的十六进制值,以便我可以更好地进行调试?或者如果任何人有更好的主意随意建议,谢谢

回答

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[] = "abcdef"; 
    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,表明它遇到的输入流的结尾。

+0

该功能很有帮助,谢谢。你也是对的,只有前80个字节被SDL_RWread读取 – Hoofamon