C++常用工具函数-1
1、转为16进制
unsigned long temp = 16;
std::cout<< "temp2="<<std::setbase(16)<< temp << std::endl;
2、数组转指针操作
unsigned char W[4*8*15]; // the expanded key
unsigned int * Wb = reinterpret_cast<unsigned int*>(W);
3、字符串转hex
char *Data = "12340987654321"
int DataLen = strlen(Data);
byte * buffer = new byte[DataLen];
for (int i = 0; i < DataLen; i += 2)
{
unsigned int value(0);
sscanf_s(Data + i, "%02X", &value);
buffer[i / 2] = (byte)value;
}
4、hex打印
typedef unsigned char byte;
byte *cBuffer = NULL;
cBuffer = new byte[32];
for (int i = 0; i < 32; i++) {
?std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(cBuffer[i]) << "";
}
5、dump
ofstream out;
out.open("Tables.dat");
if (out.is_open() == true)
{
DumpCharTable(out,"gf2_8_inv", gf2_8_inv, 256);
out << "\n\n";
DumpCharTable(out,"byte_sub", byte_sub, 256);
out << "\n\n";
DumpCharTable(out,"inv_byte_sub", inv_byte_sub, 256);
out << "\n\n";
DumpLongTable(out,"RCon", Rcon, 60);
out << "\n\n";
DumpLongTable(out,"T0", T0, 256);
out << "\n\n";
DumpLongTable(out,"T1", T1, 256);
out << "\n\n";
DumpLongTable(out,"T2", T2, 256);
out << "\n\n";
DumpLongTable(out,"T3", T3, 256);
out << "\n\n";
DumpLongTable(out,"T4", T4, 256);
out << "\n\n";
DumpLongTable(out, "T5", T5, 256);
out << "\n\n";
DumpLongTable(out, "T6", T6, 256);
out << "\n\n";
DumpLongTable(out, "T7", T7, 256);
out << "\n\n";
DumpLongTable(out,"I0", I0, 256);
out << "\n\n";
DumpLongTable(out,"I1", I1, 256);
out << "\n\n";
DumpLongTable(out,"I2", I2, 256);
out << "\n\n";
DumpLongTable(out,"I3", I3, 256);
out << "\n\n";
DumpLongTable(out,"I4", I4, 256);
out << "\n\n";
DumpLongTable(out, "I5", I5, 256);
out << "\n\n";
DumpLongTable(out, "I6", I6, 256);
out << "\n\n";
DumpLongTable(out, "I7", I7, 256);
out.close();
}
void DumpCharTable(ostream & out, const char * name, const unsigned char * table, int length)
{ // dump the contents of a table to a file
int pos;
out << name << endl << hex;
for (pos = 0; pos < length; pos++)
{
out << "0x";
if (table[pos] < 16)
out << '0';
out << static_cast<unsigned int>(table[pos]) << ',';
if ((pos %16) == 15)
out << endl;
}
out << dec;
} // DumpCharTable
void DumpLongTable(ostream & out, const char * name, const unsigned long * table, int length)
{ // dump te contents of a table to a file
int pos;
out << name << endl << hex;
for (pos = 0; pos < length; pos++)
{
out << "0x";
if (table[pos] < 16)
out << '0';
if (table[pos] < 16*16)
out << '0';
if (table[pos] < 16*16*16)
out << '0';
if (table[pos] < 16*16*16*16)
out << '0';
if (table[pos] < 16*16*16*16*16)
out << '0';
if (table[pos] < 16*16*16*16*16*16)
out << '0';
if (table[pos] < 16*16*16*16*16*16*16)
out << '0';
out << static_cast<unsigned int>(table[pos]) << ',';
if ((pos % 8) == 7)
out << endl;
}
out << dec;
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!