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

I need help with this program. When I put input 0x7f I should get output 0b01111

ID: 3719364 • Letter: I

Question

I need help with this program. When I put input 0x7f I should get output 0b01111111. When I put 0x3f I should get 0b00111111. WHen i put 0x0 I should get 0b0. When I put 0x5 I should get 0b0101. For some reason this program doesn't include the zeros, but I need them to be included. PLEASE help me fix this.

#include <iostream>
#include <string>
using namespace std;
int main() {
               string hex,dec; // string containing hexadecimal and binary numbers
               bool invalid_number = false; // if number is invalid hexadecimal number
               // input of hexadecimal number
               cout<<" Enter a hexadecimal number : ";
               getline(cin,hex);
               dec=""; // assign dec to empty string
               // since the first 2 character of hexadecimal starts from 0x, ignore them during conversion
               for(size_t i=2;i<hex.length();i++)
               {
                              if(invalid_number) // if any character is invalid exit from loop
                                             break;
                              // switch command to convert the hexadecimal character to binary strings
                              switch(hex.at(i))
                              {
                              case '0' : dec = dec + "0000" ;
                                                                           break;
                              case '1' : dec = dec + "0001";
                                                                           break;
                              case '2' : dec = dec + "0010";
                                                                           break;
                              case '3' : dec = dec + "0011";
                                                                           break;
                              case '4' : dec = dec + "0100";
                                                                           break;
                              case '5' : dec = dec + "0101";
                                                                           break;
                              case '6' : dec = dec + "0110";
                                                                           break;
                              case '7' : dec = dec + "0111";
                                                                           break;
                              case '8' : dec = dec + "1000";
                                                                           break;
                              case '9' : dec = dec + "1001" ;
                                                                           break;
                              case 'a' :
                              case 'A' :              dec = dec + "1010";
                                                                           break;
                              case 'b' :
                              case 'B' : dec = dec + "1011";
                                                                           break;
                              case 'c' :
                              case 'C' : dec = dec + "1100" ;
                                                                           break;
                              case 'd' :
                              case 'D' : dec = dec + "1101";
                                                                           break;
                              case 'e' :
                              case 'E' : dec = dec + "1110";
                                                                           break;
                              case 'f' :
                              case 'F' : dec = dec + "1111";
                                                                           break;
                              default: cout<<" Invalid hexadecimal number";
                                                                           invalid_number = true;
                                                                           break;
                              }
               }
               // if number is not invalid then remove the leading zeros from binary numbar and add 0b at the start
               if(!invalid_number)
               {
                              for(size_t i=0;i<dec.length();i++)
                              {
                                             if(dec.at(i)!='0')
                                                            break;
                                             else{
                                                            dec.erase(i,1);
                                                            i--;
                                             }
                              }
                              dec = "0b"+dec;
                              cout<<" Your number in binary is "<<dec;
               }
               return 0;
}

Explanation / Answer

You have a code which removing the leading zeroes and as per your question you dont want to get your leading zeroes removed. I have commented the code and it is working fine. For a special case which is 0x0, I have added one if block. Below is your code and bold part is the updated code.

Please do rate positive If was able to help

#include <iostream>

#include <string>

using namespace std;

int main() {

string hex,dec; // string containing hexadecimal and binary numbers

bool invalid_number = false; // if number is invalid hexadecimal number

// input of hexadecimal number

cout<<" Enter a hexadecimal number : ";

getline(cin,hex);

dec=""; // assign dec to empty string

// since the first 2 character of hexadecimal starts from 0x, ignore them during conversion

for(size_t i=2;i<hex.length();i++)

{

if(invalid_number) // if any character is invalid exit from loop

break;

// switch command to convert the hexadecimal character to binary strings

switch(hex.at(i))

{

case '0' : dec = dec + "0000" ;

break;

case '1' : dec = dec + "0001";

break;

case '2' : dec = dec + "0010";

break;

case '3' : dec = dec + "0011";

break;

case '4' : dec = dec + "0100";

break;

case '5' : dec = dec + "0101";

break;

case '6' : dec = dec + "0110";

break;

case '7' : dec = dec + "0111";

break;

case '8' : dec = dec + "1000";

break;

case '9' : dec = dec + "1001" ;

break;

case 'a' :

case 'A' : dec = dec + "1010";

break;

case 'b' :

case 'B' : dec = dec + "1011";

break;

case 'c' :

case 'C' : dec = dec + "1100" ;

break;

case 'd' :

case 'D' : dec = dec + "1101";

break;

case 'e' :

case 'E' : dec = dec + "1110";

break;

case 'f' :

case 'F' : dec = dec + "1111";

break;

default: cout<<" Invalid hexadecimal number";

invalid_number = true;

break;

}

}

// if number is not invalid then remove the leading zeros from binary numbar and add 0b at the start

if(!invalid_number)

{

/*for(size_t i=0;i<dec.length();i++)

{

if(dec.at(i)!='0')

break;

else{

dec.erase(i,1);

i--;

}

}*/

if(dec == "0000") {

dec = "0";

}

dec = "0b"+dec;

cout<<" Your number in binary is "<<dec;

}

return 0;

}

UPDATED CODE FOR SPECIAL CASE i.e 0x1c must have 5 bits

#include <iostream>

#include <string>

using namespace std;

int main() {

string hex,dec; // string containing hexadecimal and binary numbers

bool invalid_number = false; // if number is invalid hexadecimal number

// input of hexadecimal number

cout<<" Enter a hexadecimal number : ";

getline(cin,hex);

dec=""; // assign dec to empty string

// since the first 2 character of hexadecimal starts from 0x, ignore them during conversion

for(size_t i=2;i<hex.length();i++)

{

if(invalid_number) // if any character is invalid exit from loop

break;

// switch command to convert the hexadecimal character to binary strings

switch(hex.at(i))

{

case '0' : dec = dec + "0000" ;

break;

case '1' : dec = dec + "0001";

break;

case '2' : dec = dec + "0010";

break;

case '3' : dec = dec + "0011";

break;

case '4' : dec = dec + "0100";

break;

case '5' : dec = dec + "0101";

break;

case '6' : dec = dec + "0110";

break;

case '7' : dec = dec + "0111";

break;

case '8' : dec = dec + "1000";

break;

case '9' : dec = dec + "1001" ;

break;

case 'a' :

case 'A' : dec = dec + "1010";

break;

case 'b' :

case 'B' : dec = dec + "1011";

break;

case 'c' :

case 'C' : dec = dec + "1100" ;

break;

case 'd' :

case 'D' : dec = dec + "1101";

break;

case 'e' :

case 'E' : dec = dec + "1110";

break;

case 'f' :

case 'F' : dec = dec + "1111";

break;

default: cout<<" Invalid hexadecimal number";

invalid_number = true;

break;

}

}

// if number is not invalid then remove the leading zeros from binary numbar and add 0b at the start

if(!invalid_number)

{

if(hex.at(2) == '1' && hex.length() > 3) {

for(size_t i=0;i<dec.length();i++)

{

if(dec.at(i)!='0')

break;

else{

dec.erase(i,1);

i--;

}

}

}

/**/

if(dec == "0000") {

dec = "0";

}

dec = "0b"+dec;

cout<<" Your number in binary is "<<dec;

}

return 0;

}