Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++, how would you take a string of 1\'s and 0\'s for binary(8 bits) and conv

ID: 638626 • Letter: I

Question

In C++, how would you take a string of 1's and 0's for binary(8 bits) and convert this string into it's ascii value? I understand a for loop or while loop needs to be done but I can't figure out how to do the converting. I know how to convert from characters to binary but not the other way around?

Below is what I need help figuring out.

"take a string of ASCII values in base 2, and print the characters corresponding to those values back to the user. You must make sure that the user enters only 0s and 1s for their binary values, and print an error message if the user failed to supply a base 2 number. If the user enter something other than a 1 or 0, then you need to print an error message."


Read a string of 1s and 0s from the user and turn it into the corresponding text/ASCII values.

EDIT:

I needed to get a string from someone that is only 1's and 0's and treat that string as base 2. Then translate that string into text/ASCII values.

Explanation / Answer

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

void B_to_A(const char*);   //function for converting Binary to ASCII

int main(void)
{
   cout << " ASCII/Binary Converter" << endl;
   cout << " ============================ " << endl;
  
   char response;   //used to store user's response to the next question
   char garbage[50];   //used to clear entry buffer in case user types more than 1 character
   cout << " 2. Binary to ASCII Converter" << endl;
      
   while(response != '1' && response != '2' && response != 'N' && response != 'n')
   //loop if the user's response is none of the above.
   {
       cout << " Enter "1" or "2" to specify task. Enter "N" to quit: ";
       cin >> response;    //store the user's response in "response"
       cin.getline(garbage, 50);    //get rid of the entire line from the entry buffer
      
       if(response != '1' && response != '2' && response != 'N' && response != 'n')
       //will fun if user enteres unrecognized entry for "response"
           cout << " The value you entered was invalid. Please try again" << endl;
   }
  
   if(response == 'N' || response == 'n') //if the user chooses to quit
   {
       cout << "User cancel" << endl;
       exit(0);
       //output text informing termination, terminate program, return "0" for no error.
   }
  
   char text[501];
   cout << " Enter the text/code you want to convert: ";
   cin >> text;
  
   if(response == '1')
   {
       B_to_A(text);    //if the user chose Binary -->ASCII, to go B_to_A()
   }

   cout << " Conversion finished" << endl;
  
   return 0;
}

void B_to_A(const char* input)
{
   int length = strlen(input);     //get length of string
  
   int binary[8];    //array used to store 1 byte of binary number (1 character)
   int asciiNum = 0;      //the ascii number after conversion from binary
   char ascii;      //the ascii character itself
  
   cout << " ";
  
   int z = 0;   //counter used
  
   for(int x = 0; x < length / 8; x++)     //reading in bytes. total characters = length / 8
   {
       for(int a = 0; a < 8; a++)      //store info into binary[0] through binary[7]
       {
           binary[a] = (int) input[z] - 48;      //z never resets
           z++;
       }
      
       int power[8];    //will set powers of 2 in an array
       int counter = 7;        //power starts at 2^0, ends at 2^7
       for(int x = 0; x < 8; x++)
       {
           power[x] = counter;      //power[] = {7, 6, 5, ..... 1, 0}
           counter--;    //decrement counter each time
       }
      
       for(int y = 0; y < 8; y++)    //will compute asciiNum
       {
           double a = binary[y];    //store the element from binary[] as "a"
           double b = power[y];    //store the lement from power[] as "b"
          
           asciiNum += a* pow(2, b);   //asciiNum = sum of a * 2^power where 0 <= power <= 7, power is int
       }
      
       ascii = asciiNum;   //assign the asciiNum value to ascii, to change it into an actual character
       asciiNum = 0;    //reset asciiNum for next loop
      
       cout << ascii;   //display the ascii character
   }
}