这是我目前所拥有的:
void WriteHexToFile( std::ofstream &stream, void *ptr, int buflen, char *prefix )
{
unsigned char *buf = (unsigned char*)ptr;
for( int i = 0; i < buflen; ++i ) {
if( i % 16 == 0 ) {
stream << prefix;
}
stream << buf[i] << ' ';
}
}
我尝试过使用stream.hex、stream.setf( std::ios::hex ),以及在Google 上搜索一下。我也尝试过:
stream << stream.hex << (int)buf[i] << ' ';
但这似乎也不起作用。
以下是它当前产生的一些输出的示例:
Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í
Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í
Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í
Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í
Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í
Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í
我希望输出如下所示:
FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00
FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00
FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00
FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00
FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00
FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00
#include <iostream>
int main() {
char c = 123;
std::cout << std::hex << int(c) << std::endl;
}
Edit:用零填充:
#include <iostream>
#include <iomanip>
int main() {
char c = 13;
std::cout << std::hex << std::setw(2) << std::setfill('0') << int(c) << std::endl;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)