2016-09-22 137 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