采用txt文件读写方式进行,将16进制字符串转换为2进制字符串

有一个存储16进制字符串(除了16进制数之外还有其他内容)的txt文件:
1.提取16进制数,写入到txt文件中;
2.提取出来的16进制数转换为2进制字符串,并写入到txt文件中。

比如,原txt文件内容如下所示,要将其中的16进制数提取出来,并将其转换为2进制存入文件之中。
// ---- --------------------- ---- //
// Generator Matrix for LDPC (1280,1024) //
// ---- --------------------- ---- //
// ---- SuperColumn 1 ---- //
GM3_R1_C1 = 32’h4569b8f3,
GM3_R2_C1 = 32’h523726a1,
GM3_R3_C1 = 32’hf4e0049b,
GM3_R4_C1 = 32’h1c043435,
GM3_R5_C1 = 32’h9963415c,
GM3_R6_C1 = 32’h5695275d,
GM3_R7_C1 = 32’h437cc0a1,
GM3_R8_C1 = 32’h912a3bc8,
GM3_R9_C1 = 32’h482bf8a9,
GM3_R10_C1 = 32’he6f283ec,
GM3_R11_C1 = 32’hd3fc6890,
GM3_R12_C1 = 32’hbaee7dde,
GM3_R13_C1 = 32’hf21b2779,
GM3_R14_C1 = 32’h20f631d7,
GM3_R15_C1 = 32’h0fdb2a7b,
GM3_R16_C1 = 32’he1daeaae,
GM3_R17_C1 = 32’hc7a09bb3,
GM3_R18_C1 = 32’h4662aee9,
GM3_R19_C1 = 32’h30d5fe05,
GM3_R20_C1 = 32’hb384e075,
GM3_R21_C1 = 32’h5e88852a,
GM3_R22_C1 = 32’h39bfbf8c,
GM3_R23_C1 = 32’h3430f8ac,
GM3_R24_C1 = 32’hcd2e01ff,
GM3_R25_C1 = 32’h5cc50bef,
GM3_R26_C1 = 32’hd01dd21b,
GM3_R27_C1 = 32’h284ff145,
GM3_R28_C1 = 32’h7b2b80ab,
GM3_R29_C1 = 32’h27220d57,
GM3_R30_C1 = 32’h5c2dd82b,
GM3_R31_C1 = 32’h07fbfcbb,
GM3_R32_C1 = 32’h7a9fbfa6,
// ---- SuperColumn 2 ---- //
GM3_R1_C2 = 32’h9d5c20bf,
GM3_R2_C2 = 32’hc3c2d8fc,
GM3_R3_C2 = 32’h63029293,
GM3_R4_C2 = 32’hea38302b,
GM3_R5_C2 = 32’h2df43901,
GM3_R6_C2 = 32’h68f496d4,
GM3_R7_C2 = 32’h29e77616,
GM3_R8_C2 = 32’h737c1d0b,
GM3_R9_C2 = 32’h15625165,
GM3_R10_C2 = 32’h6bb52743,
GM3_R11_C2 = 32’h7b28d6a8,
GM3_R12_C2 = 32’h322fa027,
GM3_R13_C2 = 32’h2ca10e6f,
GM3_R14_C2 = 32’h7640119c,
GM3_R15_C2 = 32’ha0fea7fc,
GM3_R16_C2 = 32’h5b3079bc,
GM3_R17_C2 = 32’h20f3faa9,
GM3_R18_C2 = 32’h49b41b82,
GM3_R19_C2 = 32’h44e8b7a5,
GM3_R20_C2 = 32’habf6668c,
GM3_R21_C2 = 32’h040b69b7,
GM3_R22_C2 = 32’hb6563006,
GM3_R23_C2 = 32’ha6a5f7d1,
GM3_R24_C2 = 32’hf250511d,
GM3_R25_C2 = 32’hafaffb7a,
GM3_R26_C2 = 32’h0a93bab3,
GM3_R27_C2 = 32’hc01bb87f,
GM3_R28_C2 = 32’h3054a2c7,
GM3_R29_C2 = 32’h4ca153b4,
GM3_R30_C2 = 32’h84368bb7,
GM3_R31_C2 = 32’h47e9f4f0,
GM3_R32_C2 = 32’h576e3087,
// ---- SuperColumn 3 ---- //


实现的代码如下,其中函数string hex2bin(const string& hex);
是在GitHub上找到的,当时直接复制过来的,具体链接忘记在哪里了。
/

引用文本


#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdexcept>
#include <vector> 
#include <string> 
#include <fstream> 

using namespace std;

string hex2bin(const string& hex);

string hex2bin(const string& hex)
{
	string bin;
	std::string::const_iterator itr = hex.begin();

	while (itr != hex.end()) {
		switch (*itr) {
		case '0':
			bin += "0000";
			break;
		case '1':
			bin += "0001";
			break;
		case '2':
			bin += "0010";
			break;
		case '3':
			bin += "0011";
			break;
		case '4':
			bin += "0100";
			break;
		case '5':
			bin += "0101";
			break;
		case '6':
			bin += "0110";
			break;
		case '7':
			bin += "0111";
			break;
		case '8':
			bin += "1000";
			break;
		case '9':
			bin += "1001";
			break;
		case 'A': case 'a':
			bin += "1010";
			break;
		case 'B': case 'b':
			bin += "1011";
			break;
		case 'C': case 'c':
			bin += "1100";
			break;
		case 'D': case 'd':
			bin += "1101";
			break;
		case 'E': case 'e':
			bin += "1110";
			break;
		case 'F': case 'f':
			bin += "1111";
			break;
		default:
			throw domain_error("Input does not look like hex");
		}
		++itr;
	}

	return bin;
}

int main()
{
	
	ifstream myfile("E:\\my_simulation\\get_location\\G_Matrix Structure\\G_Matrix Structure\\32768_16384.txt");
	ofstream outfile_b("E:\\my_simulation\\get_location\\Generator_Matrix\\32768_16384_GB.txt", ios::app);
	//二进制文件存放位置
	ofstream outfile_h("E:\\my_simulation\\get_location\\Generator_Matrix\\32768_16384_GH.txt", ios::app);
	//16进制文件存放位置
	
	string temp, temp_used;
	string ans;
	int n = 0, m = 0;
	//vector location1;
	if (!myfile.is_open())
	{
		cout << "未成功打开文件" << endl;
	}
	int line = 0;
	while (getline(myfile, temp))//每次读取原文件中的一行,存入temp中
	{
		if (temp.empty()) continue;//temp为空则继续

		//判断什么条件开始读文件里的内容(即16进制数),根据需要选择。此处条件是“‘h”之后开始读取,每行的最后一个字符是“,”,舍弃不读
		for (int i = 0; i < temp.size(); i++) {
			if ((temp[i] == '\'') && (temp[i + 1] == 'h')) {
				line++;
				for (int j = 0; j < (temp.size() - i - 3); j++) {
					temp_used.push_back(temp[i + j + 2]);
					//将选择后的16进制数存入temp_used中
				}
				break;
			}
		}

		if (temp_used.empty()) continue;//temp_used为空则继续

		vector <int> Binary;


		try {
			ans = hex2bin(temp_used);//将16进制字符串转化为2进制字符串
		}
		catch (domain_error error) {
			cout << error.what();
		}
		//向文件输出16进制数
		for (int i = 0; i < temp_used.size(); i++) {
			outfile_h << temp_used[i];
		}
		outfile_h << endl;
		if (line % 32 == 0)    
			outfile_h << endl << endl;         
		//根据需要进行输出,使其适应原文件中的要求
		//比如原文件中,32行16进制数为一个块,就输出32行后,为了表示清晰,插入空行
			

		temp_used.erase(temp_used.begin(), temp_used.end());
		//清除temp_used里的内容,便于下一次使用。不清除的话,将在原来序列的后面继续
		//添加下一行的16进制数,造成错误

		//
		for (n = 0; n < ans.size(); n++)
		{
			char b = ans[n];
			if (b == '1') {
				Binary.push_back(1);
			}
			else if (b == '0') {
				Binary.push_back(0);
			}
			else {
				Binary.push_back(7);//错误输出指示,7无明显意义,可用任何数代替
			}
		}

		//向目标文件写入2进制数字
		for (int i = 0; i < Binary.size(); i++) {
			outfile_b << Binary[i] << " ";
		}
		Binary.clear();
		outfile_b << endl;
		if (line % 32 == 0)           
			outfile_b << endl << endl;
		//根据需要进行输出,使其适应原文件中的要求
		//比如原文件中,32行16进制数为一个块,就输出32行后,为了表示清晰,插入空行

		

	}


	myfile.close();/关闭文件
	outfile_b.close();
	outfile_h.close();

	return 0;
}

你可能感兴趣的:(16进制转化为2进制,C++,LDPC,生成矩阵)