将二进制字符串转换为ASCII

时间:2022-07-20 19:46:51

I am trying to convert a string of binary to a readable ASCII string. I am able to successfully output the converted string to the console as readable text but when I try to output it to a file, it outputs it as non-English characters.

我试图将二进制字符串转换为可读的ASCII字符串。我能够将转换后的字符串成功输出到控制台作为可读文本,但是当我尝试将其输出到文件时,它会将其输出为非英文字符。

void toHexx(string& in, ofstream& outFile)
{
   int temp[8]={0};
   char theChar[8];
   stringstream ss;

   for(int i=0;i<8;i++)
   {
      if(in.at(0)=='1')
         temp[i]+=128;
      if(in.at(1)=='1')
         temp[i]+=64;
      if(in.at(2)=='1')
         temp[i]+=32;
      if(in.at(3)=='1')
         temp[i]+=16;  
      if(in.at(4)=='1')
         temp[i]+=8;
      if(in.at(5)=='1')
         temp[i]+=4;
      if(in.at(6)=='1')
         temp[i]+=2;
      if(in.at(7)=='1')
         temp[i]+=1;

      in.erase(0,8);

       theChar[i]=(char) temp[i];

       ss << theChar[i];

   }

    cout<<ss.str();
    outFile << ss.str();
}

1 个解决方案

#1


0  

What your'e doing here is not hex conversion (hex is a different way of interpreting and presenting the data. You're storing the information in an integer array where each cell represents a bit. First - if you expect a byte, which can hold a value up to 255, why would you declare temp as int[8] ? Instead, you could hold that in 'char'. In addition, you have several more problems, with type-selection for your parameters and some logic problems. I've modified your function a little to perform what I think you've wanted, look at the diff and try to see where you mistaken. Good luck!

你在这里做的不是十六进制转换(十六进制是一种不同的解释和呈现数据的方式。你将信息存储在一个整数数组中,每个单元代表一个位。首先 - 如果你期望一个字节,可以保持一个最大值为255的值,你为什么要将temp声明为int [8]?相反,你可以在'char'中保存它。此外,你还有几个问题,你的参数的类型选择和一些逻辑问题。我已经修改了你的功能以执行我认为你想要的功能,看看差异并试着看看你错在哪里。祝你好运!

void binToChar(const std::string& in)
{
    char temp = 0;
    for (int i = 0; i<8; i++)
    {
        if ('1' == in.at(i)) {
            temp += pow(2, 7 - i);
        }
    }

    std::cout << temp;
}

#1


0  

What your'e doing here is not hex conversion (hex is a different way of interpreting and presenting the data. You're storing the information in an integer array where each cell represents a bit. First - if you expect a byte, which can hold a value up to 255, why would you declare temp as int[8] ? Instead, you could hold that in 'char'. In addition, you have several more problems, with type-selection for your parameters and some logic problems. I've modified your function a little to perform what I think you've wanted, look at the diff and try to see where you mistaken. Good luck!

你在这里做的不是十六进制转换(十六进制是一种不同的解释和呈现数据的方式。你将信息存储在一个整数数组中,每个单元代表一个位。首先 - 如果你期望一个字节,可以保持一个最大值为255的值,你为什么要将temp声明为int [8]?相反,你可以在'char'中保存它。此外,你还有几个问题,你的参数的类型选择和一些逻辑问题。我已经修改了你的功能以执行我认为你想要的功能,看看差异并试着看看你错在哪里。祝你好运!

void binToChar(const std::string& in)
{
    char temp = 0;
    for (int i = 0; i<8; i++)
    {
        if ('1' == in.at(i)) {
            temp += pow(2, 7 - i);
        }
    }

    std::cout << temp;
}