mardi 4 août 2015

string handling in c and C++ mix programming

I want to use an API from a library. I am confused about its second argument.

    cs_disasm(handle,(const uint8_t*)("\xff\x43\x12\xd1"),4 , 0x0, 1, &insn);

The above code works fine. "\xff\x43\x12\xd1", this string represents a machine code. I want this API to accept arbitrary machine code. What I have now is an

uint32_t machine_code. I use it as follow, but not work.
std::stringstream ss;
ss<< std::hex  << setfill('0') << setw(2) <<  (int)(machine_code&0xff); // int decimal_value
std::string res1 ( ss.str() );
ss.str(std::string());
//cout << res1 << endl;

ss<< std::hex << setfill('0') << setw(2) << (int)((machine_code>>8)&0xff); // int decimal_value
std::string res2 ( ss.str() );
ss.str(std::string());


ss<< std::hex << setfill('0') << setw(2)  << (int)((machine_code>>16)&0xff); // int decimal_value
std::string res3 ( ss.str() );
ss.str(std::string());

ss<< std::hex << setfill('0') << setw(2) << (int)((machine_code>>24)&0xff); // int decimal_value
std::string res4 ( ss.str() );
string modified_machine_code = "\\x"+ res1 +"\\x"+  res2 +"\\x"+ res3 +"\\x"+ res4;
cs_disasm(hao_handle,(const uint8_t*)(modified_machine_code.c_str()),4 , 0x0, 1, &hao_insn);

What is the problem with my code? If you have a better solution, that is also great.

Aucun commentaire:

Enregistrer un commentaire