場景:1. 在讀取檔案或記憶體時,有時候需要輸出那段記憶體的十六或二進位制表示進行分析。
2. 標準的printf沒有顯示二進位制的,而%x顯示有最大上限,就是8位元組,超過8位元組就不行了。
test_binary_hex.cpp
[cpp]view plain
copy
print?
#include
#include
#include
#include
#include
#include
std::string tobinarystring(const uint8_t* buf,int len)
; for(int i = output_len - 1,j = 0; i >=0 ; --i,++j)
return output;
} std::string tohexstring(const uint8_t* buf,int len,std::string tok = "")
return output;
} int main(int argc, char
const *argv)
; std::string output = tobinarystring(buf,2);
std::string output_hex = tohexstring(buf,2,":");
std::cout << output << std::endl;
std::cout << output_hex << std::endl;
assert(!strcmp(output.c_str(),"1000000000001111"));
output = tobinarystring(buf,3);
std::cout << output << std::endl;
assert(!strcmp(output.c_str(),"100000000000111101010001"));
output = tobinarystring(buf,4);
assert(!strcmp(output.c_str(),"10000000000011110101000111101110"));
std::cout << output << std::endl;
output = tobinarystring(buf,5);
assert(!strcmp(output.c_str(),"1000000000001111010100011110111010100111"));
std::cout << output << std::endl;
return 0;
}
#include #include #include #include #include #include std::string tobinarystring(const uint8_t* buf,int len)
; for(int i = output_len - 1,j = 0; i >=0 ; --i,++j)
return output;
}std::string tohexstring(const uint8_t* buf,int len,std::string tok = "")
return output;
}int main(int argc, char const *argv)
; std::string output = tobinarystring(buf,2);
std::string output_hex = tohexstring(buf,2,":");
std::cout << output << std::endl;
std::cout << output_hex << std::endl;
assert(!strcmp(output.c_str(),"1000000000001111"));
output = tobinarystring(buf,3);
std::cout << output << std::endl;
assert(!strcmp(output.c_str(),"100000000000111101010001"));
output = tobinarystring(buf,4);
assert(!strcmp(output.c_str(),"10000000000011110101000111101110"));
std::cout << output << std::endl;
output = tobinarystring(buf,5);
assert(!strcmp(output.c_str(),"1000000000001111010100011110111010100111"));
std::cout << output << std::endl;
return 0;
}
輸出:
[plain]view plain
copy
print?
0x01
1000000000001111
0x80:0x0f:
100000000000111101010001
10000000000011110101000111101110
1000000000001111010100011110111010100111
二進位制輸出
將1 byte 數,以二進位制輸出.注意是1byte數,如果是int之類的,應注意大小端問題.include define bytetobinarypattern d d d d d d d d define bytetobinary byte byte 0x80 1 0 byte 0x40 1 0...
二進位制,八進位制,十六進製制輸出c c
首先介紹簡單的八進位制和十六進製制輸出 只要通過格式控制符就可以了 八進位制 o include using namespace std intmain 執行結果 十六進製制 x include using namespace std intmain 執行結果 額外一提 x表示字母部分是小寫,x則是...
操作二進位制寫入二進
操作二進位制 寫入二進位制 1 宣告變數 sqlite3 stmt stat 2 把sql語句解析到stat結構中去 sqlite3 prepare 3 繫結替換 sqlite3 bind blob 4 儲存到資料庫 int result sqlite3 step 5 釋放stat結構 sqlite...