将文本文件转换为bin/hex/dec格式



我一直在尝试编写一个从文件中读取的程序,如果它读取B,则意味着下一行是二进制,并且它将下一行从二进制转换为十六进制和dec并打印出二进制,然后移动到下一行。我有问题#1,去下一行和#2有我的函数翻译。

#include <iostream>
#include <sstream>
#include <fstream>
#include <bitset>
#include <cmath>
#include <string.h>
#include <cstring>
#include <string>
using namespace std;
//creating function to tranlsate hex into decimal
int hexi(char num[]) {
int len = strlen(num);
int base = 1;
int temp = 0;
for (int i = len - 1; i >= 0; i--) {
if (num[i] >= '0' && num[i] <= '9') {
temp += (num[i] - 48) * base;
base = base * 16;
}
else if (num[i] >= 'A' && num[i] <= 'F') {
temp += (num[i] - 55) * base;
base = base * 16;
}
}
return temp;
}
int main()
{

{
string line;
signed char Binary[6] = { }; //array for binary string
char Hex[16] = {}; //array for hex string
double decimal{}; // initilizing decimal value
char letter; //letter entered
int rem; //remainder
unsigned int i = 0; //for binary
long long n; //for string of 010
ifstream file;
file.open("C:\Users\18059\source\repos\translation\input.txt");
while (true)
{
getline(file, line);
if (file.eof()) break;
cout << line << " Binary: " << Binary << bitset<6>{i} << " Hexidecimal: " <<  hexi(Hex) << line << " Decimal: " << decimal << endl;
//getline(file, line);

{
//read the letter
cin >> letter;
//reading if B for binary
if (letter == 'B')
{
cout << endl;
file >> n;
while (n != 0) {
rem = n % 10;
n /= 10;
decimal += rem * pow(2, i);
++i;
}
cout << "Decimal: " << decimal << endl;
}
else if (letter == 'H')
{
file >> Hex;
cout << Hex << "Hexidecimal: " << hexi(Hex) << endl;
return 0;
}
}
{
if (letter == 'D')
{
file >> decimal;
cout << "Decimal: " << dec << endl;
}
}
}
file.close();
}
}

现在它打印这个输出内容

然后我的文本文件看起来像这个

有一些问题。

你把你的生活弄得太复杂了。可以使用现有的转换函数

但是,你在代码中也有一些错误。你总是读一行,这一行已经是字母了。然后你再读一遍,想要这封信。但是你已经读到下一行了。所以,它永远不会工作。

另外,你有一个错字,读std::cin的信而不是file的信。这将阻止程序的执行,直到您在控制台中输入一个字母。但是,因为你甚至没有意识到这一点,你将永远等待。

你需要做的:

  • 逐行读取
  • 检查是否以字母
  • 开头
  • 根据字母,记住下一轮
  • 的转换模式
  • 如果没有字母,而是数字,那么将行字符串转换为数字,使用数字基数
  • 使用
  • 的现有函数

程序可以看起来像这样(一百万种可能的解决方案之一):

#include <iostream>
#include <fstream>
#include <bitset>
#include <string>
#include <iomanip>
int main()
{
std::string line;
enum Mode {decimal, binary,hex};
Mode mode = Mode::decimal;
std::ifstream file("r:\input.txt");
if (!file) {
std::cerr << "nError: coud not open source filen";
}
else while (std::getline(file, line))
{
//read the the first character and check, if it is a valid letter
char letter = line[0];
switch (letter) {
case 'D':
mode = Mode::decimal;
break;
case 'B':
mode = Mode::binary;
break;
case 'H':
mode = Mode::hex;
break;
default:
// No letter, so, we assume a number
// Use last read mode
long value{};
switch (mode) {
case Mode::decimal:
value = std::stol(line, nullptr, 10);
break;
case Mode::binary:
value = std::stol(line, nullptr, 2);
break;
case Mode::hex:
value = std::stol(line, nullptr, 16);
break;
default:
std::cerr << "nError: invalid Moden";
break;
}
std::cout << line << "  t  " 
<< " Binary: " << std::bitset<6>{static_cast<unsigned long long>(value)} 
<< " tHexidecimal: " << std::hex << value
<< " tDecimal: " << std::dec << value << std::endl;
}
}
return 0;
}

谢谢你的帮助,我肯定都用过了。我没有翻译所有内容然后打印在一行中,而是使用开关箱为我翻译行,然后打印每个开关箱的输出。

//if file is open
if (file.is_open())
{
//while there is a line to read
while (getline(file, big))
{
letter = big[0];
//switch case between b h and d
switch (letter)
{
//binary
case 'B':
//get bitstring
file >> bit;
//convert the bit to ulong
cout << "Binary: " << bit << " Hexidecimal: " << hex << bit.to_ulong() << " Decimal: " 
<< dec << bit.to_ulong() << endl;
break;
case 'H':
//define and get char
char hexi;
file >> hexi;
//use hex table numbers from whatever char to translate
//for decimal use hex table
cout << "Binary: " << bitset<6>{(unsigned(tableConverter(hexi)))} << " Hexidecimal: "
<< hex << hexi << " Decimal: " << to_string(tableConverter(hexi)) << endl;
break;
case 'D':
//get number
file >> number;
//using normal bitset, hex, and dec operations
cout << "Binary: " << bitset<6>{unsigned(number)}
<< " Hexidecimal: " << hex << number << " Decimal: " << dec << number << endl;
break;
default:
break;

对于十六进制,我不能很好地找出翻译,所以我只是做了一个表格,其中a=10, b=11,等等,并使用它

最新更新