一个欲儿的博客

一个欲儿的博客

C++ 10进制和16进制的互转
2022-06-09

成功封装为函数,方便调用,其次本程序,只能实现16进制在8位以内的转换,也就是说16进制值必须 <= FFFFFFFF ,当然了对应的10进制值必须 <= 4294967295.其实已经很大很够用拉

下面是代码:代码太多,建议换个头文件

#include <string>
#include <iostream>
#include <stdio.h>
using namespace std;


//10到16 用法在下面
/*
	//10进制转到16进制
	unsigned int typeten = 4294967295;
	string result = tentosixteen(typeten);
	cout << "10进制转到16进制结果:" << result << endl;
*/

int quzheng(unsigned int tendoc)
{
	int qian = tendoc / 16;
	return qian;
}

string huanzhi(int numtype)
{
	string us = std::to_string(numtype);
	if (us == "0")
	{
		string result = "0";
		return result;
	}
	else if (us == "1")
	{
		string result = "1";
		return result;
	}
	else if (us == "2")
	{
		string result = "2";
		return result;
	}
	else if (us == "3")
	{
		string result = "3";
		return result;
	}
	else if (us == "4")
	{
		string result = "4";
		return result;
	}
	else if (us == "5")
	{
		string result = "5";
		return result;
	}
	else if (us == "6")
	{
		string result = "6";
		return result;
	}
	else if (us == "7")
	{
		string result = "7";
		return result;
	}
	else if (us == "8")
	{
		string result = "8";
		return result;
	}
	else if (us == "9")
	{
		string result = "9";
		return result;
	}
	else if (us == "10")
	{
		string result = "A";
		return result;
	}
	else if (us == "11")
	{
		string result = "B";
		return result;
	}
	else if (us == "12")
	{
		string result = "C";
		return result;
	}
	else if (us == "13")
	{
		string result = "D";
		return result;
	}
	else if (us == "14")
	{
		string result = "E";
		return result;
	}
	else if (us == "15")
	{
		string result = "F";
		return result;
	}
	else if (us == "16")
	{
		string result = "10";
		return result;
	}
}

string daoxu(string sixteendoc)
{
	int length = strlen(sixteendoc.c_str());
	int i = 0;
	string ret;
	string result;
	while (i < length || i == length)
	{
		ret = sixteendoc.substr(length - i, 1);
		result = result + ret;
		//cout << ret << endl;
		i = i + 1;
	}
	return result;
}

string tentosixteen(unsigned int tendoc)
{
	//int tendoc = 426;
	int zhengshu = quzheng(tendoc);
	string sixteendoc = "";
	while (true)
	{
		if (zhengshu > 16)
		{
			int cha = tendoc - zhengshu * 16;
			tendoc = zhengshu;
			//cout << cha << endl;
			string result = huanzhi(cha);
			sixteendoc = sixteendoc + result;
			zhengshu = quzheng(tendoc);
		}
		else if (zhengshu == 16)
		{
			int cha = tendoc - zhengshu * 16;
			tendoc = zhengshu;
			//cout << cha << endl;
			string result = huanzhi(cha);
			sixteendoc = sixteendoc + result;
			zhengshu = quzheng(tendoc);
		}
		else
		{
			int cha = tendoc - zhengshu * 16;
			tendoc = zhengshu;
			//cout << cha << endl;
			//cout << zhengshu << endl;//最后一个整数也是差值
			string result1 = huanzhi(cha);
			string result2 = huanzhi(zhengshu);
			sixteendoc = sixteendoc + result1 + result2;
			break;
		}
	}
	string oversixdoc = daoxu(sixteendoc);//这个时候sixteendoc是反着的所以倒序倒为正了
	//cout << "oversixdoc:" << oversixdoc << endl;
	int length = strlen(oversixdoc.c_str());
	string tianchong;
	//cout <<"length:"  <<  length << endl;
	int i = 8 - length;
	while (true)
	{
		if (i > 0)
		{
			tianchong = tianchong + "0";
		}
		else
		{
			break;
		}
		i = i - 1;
	}
	//cout << "tianchong" << tianchong << endl;
	string result = tianchong + oversixdoc;
	//string result = tianchong + oversixdoc;
	return result;
}



//16到10 用法在下面
/*
	//16进制到10进制用法在下面
	string typesixteen = "FFFFFFFF";
	unsigned int result1 = sixteentoten(typesixteen);
	cout << "16进制转到10进制结果:" << result1 << endl;
*/
int beishu(int length)
{
	int ji = 1;
	int length1 = 7 - length;
	int i = 0;
	while (true)
	{
		if (i < length1)
		{
			ji = ji * 16;
		}
		else
		{
			break;
		}
		i = i + 1;
	}
	//cout << "ji : " << ji << endl;
	return ji;
}

int callback(string ret, int i)
{
	unsigned int callback = 1;
	//cout << "i:" << i << "ret" << ret << endl;
	//cout << "beishu : " << beilv << endl;
	if (ret == "0")
	{
		int beilv = beishu(i);
		callback = beilv * 0;
	}
	else if (ret == "1")
	{
		int beilv = beishu(i);
		callback = beilv * 1;
	}
	else if (ret == "2")
	{
		int beilv = beishu(i);
		callback = beilv * 2;
	}
	else if (ret == "3")
	{
		int beilv = beishu(i);
		callback = beilv * 3;
	}
	else if (ret == "4")
	{
		int beilv = beishu(i);
		callback = beilv * 4;
	}
	else if (ret == "5")
	{
		int beilv = beishu(i);
		callback = beilv * 5;
	}
	else if (ret == "6")
	{
		int beilv = beishu(i);
		callback = beilv * 6;
	}
	else if (ret == "7")
	{
		int beilv = beishu(i);
		callback = beilv * 7;
	}
	else if (ret == "8")
	{
		int beilv = beishu(i);
		callback = beilv * 8;
	}
	else if (ret == "9")
	{
		int beilv = beishu(i);
		callback = beilv * 9;
	}
	else if (ret == "A" || ret == "a")
	{
		int beilv = beishu(i);
		callback = beilv * 10;
	}
	else if (ret == "B" || ret == "b")
	{
		int beilv = beishu(i);
		callback = beilv * 11;
	}
	else if (ret == "C" || ret == "c")
	{
		int beilv = beishu(i);
		callback = beilv * 12;
	}
	else if (ret == "D" || ret == "d")
	{
		int beilv = beishu(i);
		callback = beilv * 13;
	}
	else if (ret == "E" || ret == "e")
	{
		int beilv = beishu(i);
		callback = beilv * 14;
	}
	else if (ret == "F" || ret == "f")
	{
		int beilv = beishu(i);
		callback = beilv * 15;
	}
	return callback;
}

int sixteentoten(string ten)
{
	int length = strlen(ten.c_str());
	int i = 0;
	int answer = 0;
	int reback;
	string ret;
	while (true)
	{
		if (i < length)
		{
			ret = ten.substr(i, 1);//获取到了每一个位数上的具体值
			reback = callback(ret,i);
			answer = answer + reback;
		}
		else
		{
			break;
		}
		i = i + 1;
	}



	string result = "1";
	return answer;
}


int main()
{
	//10进制转到16进制
	unsigned int typeten = 4294967295;
	string result = tentosixteen(typeten);
	cout << "10进制转到16进制结果:" << result << endl;

	//16进制到10进制用法在下面
	string typesixteen = "FFFFFFFF";
	unsigned int result1 = sixteentoten(typesixteen);
	cout << "16进制转到10进制结果:" << result1 << endl;
	
	while (true);
}


发表评论: