C++常用工具函数-1

本文详细介绍了C++中将数值转换为16进制、数组操作技巧、字符串转为hex表示以及hex格式的输出,包括使用`std::setbase`、`reinterpret_cast`、`scanf_s`和文件流操作(如`ofstream`)来处理字符和长表的dump功能。
摘要由CSDN通过智能技术生成

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;
	}

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值