ในที่นี้เราจะมาดูวิธีการซ่อนสตริงบางส่วนในโค้ดไบนารี (ในที่นี้โค้ดไบนารีจะแสดงเป็นเลขฐานสิบหก)
วิธีการนั้นง่ายมาก เราสามารถใช้สตรีมสตริงเพื่อแปลงเลขฐานสิบเป็นเลขฐานสิบหก จากสตริง เราจะอ่านอักขระแต่ละตัว และใช้ค่า ASCII ของมัน ค่า ASCII เหล่านี้จะถูกแปลงเป็นค่าเลขฐานสิบหก จากนั้นเราก็สามารถพิมพ์ได้ทีละรายการ
ตัวอย่าง
#include<iostream>
#include<sstream>
using namespace std;
string dec_to_hex(int decimal){ //function is used to convert decimal to hex
stringstream my_ss;
my_ss << hex << decimal;
return my_ss.str();
}
main(){
string my_string = "This is a sample text";
for(int i = 0; i<my_string.length(); i++){
cout << dec_to_hex(my_string.at(i)) << " ";
}
} ผลลัพธ์
54 68 69 73 20 69 73 20 61 20 73 61 6d 70 6c 65 20 74 65 78 74