c++中如何实现二进制(Bin)与 十六进制(Hex)之间的相互转换

c++中如何实现二进制(Bin)与十六进制(Hex)之间的相互转换

1. 二进制 -> 十六进制

#include "stdafx.h"
#include "iostream"
#include "string"

using namespace std;

//二进制 -》十六进制
string BinStrToHexStr(string str)
{
	string str_Hex = "";
	string temp_Hex = "";
	int iHex = 0;
	if (str.size() % 4 != 0)
	{
		int num = 4 - (str.size() % 4);
		for (int i = 0; i < num; i++)
		{
			str = "0" + str;
		}
	}
	for (int i = 0; i < str.size(); i += 4)
	{
		iHex = (str[i] - '0') * 8 + (str[i + 1] - '0') * 4 + (str[i + 2] - '0') * 2 + (str[i + 3] - '0') * 1;
		if (iHex >= 10)
		{
			int left = iHex % 16;
			temp_Hex = 'A' + left - 10;
		}
		else
		{
			temp_Hex = to_string(iHex);
		}
		str_Hex += temp_Hex;
	}
	return str_Hex;
}

int main()
{
	string str_2 = "1111011";
	cout << "二进制:" << str_2.c_str() << endl;
	string str_Hex = BinStrToHexStr(str_2);
	cout << "十六进制:0x" << str_Hex.c_str() << endl;
	system("pause");
}

结果验证:
在这里插入图片描述

2. 十六进制 -> 二进制

#include "stdafx.h"
#include "iostream"
#include "string"
#include "vector"
#include <algorithm>

using namespace std;

#define TOLOWER(p) {transform(p.begin(),p.end(),p.begin(),::tolower);}

//十六进制 -> 二进制
string HexStrToBinStr(string str)
{
	string str_Bin = "";
	int iDec = 0;
	TOLOWER(str);
	sscanf_s(str.c_str(), "%x", &iDec);
	vector<int>vec_remder;
	int remder = 0;
	while (iDec / 2 != 0)
	{
		remder = iDec % 2;
		vec_remder.push_back(remder);
		iDec /= 2;
	}
	remder = iDec % 2;
	vec_remder.push_back(remder);
	reverse(vec_remder.begin(), vec_remder.end());
	for (int i = 0; i < vec_remder.size(); i++)
	{
		str_Bin += to_string(vec_remder[i]);
	}
	return str_Bin;
}


int main()
{
	string str_16 = "7B";
	cout << "十六进制:0x" << str_16.c_str() << endl;
	string str_Bin = HexStrToBinStr(str_16);
	cout << "二进制:" << str_Bin.c_str() << endl;
	system("pause");
}

结果验证:
在这里插入图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的C++程序,可以实现二进制文件和十六进制文件的相互转换。该程序使用了多线程来提高转换速度。 ```c++ #include <iostream> #include <fstream> #include <sstream> #include <vector> #include <thread> #include <mutex> using namespace std; mutex mtx; // 互斥锁,用于线程安全输出 void binaryToHex(const string& inputFileName, const string& outputFileName) { ifstream inputFile(inputFileName, ios::binary); ofstream outputFile(outputFileName); stringstream ss; ss << hex << uppercase << noshowbase << noskipws; char c; while (inputFile >> c) { ss << static_cast<int>(c); } string hexString = ss.str(); for (int i = 0; i < hexString.length(); i += 2) { string byteString = hexString.substr(i, 2); outputFile << byteString << " "; } mtx.lock(); cout << "Binary to hex conversion completed!" << endl; mtx.unlock(); } void hexToBinary(const string& inputFileName, const string& outputFileName) { ifstream inputFile(inputFileName); ofstream outputFile(outputFileName, ios::binary); stringstream ss; string hexString; while (inputFile >> hexString) { int byte = stoi(hexString, nullptr, 16); outputFile << static_cast<char>(byte); } mtx.lock(); cout << "Hex to binary conversion completed!" << endl; mtx.unlock(); } int main() { string inputFileName = "input.bin"; string hexOutputFileName = "output.hex"; string binaryOutputFileName = "output.bin"; thread t1(binaryToHex, inputFileName, hexOutputFileName); thread t2(hexToBinary, hexOutputFileName, binaryOutputFileName); t1.join(); t2.join(); return 0; } ``` 该程序使用了两个函数 `binaryToHex` 和 `hexToBinary` 来实现二进制文件和十六进制文件的相互转换。这两个函数分别将输入文件转换成字符串,然后进行转换,并输出到对应的输出文件。 在 `main` 函数,我们创建了两个线程 `t1` 和 `t2` 分别用来执行转换函数。使用 `join` 方法等待线程执行完毕后,程序退出。 注意,在输出结果时,我们使用了一个互斥锁 `mtx` 来保证线程安全输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值