Question #39660

Hello, I am studying C++ and fairly new at the language, I am trying to get a better understanding on how the following code works:

#define MAX 1000

int main(){

char binaryNumber[MAX],hexaDecimal[MAX];

int temp;

long int i=0,j=0;

cout << "Enter any number any binary number: ";

cin >> binaryNumber;

while(binaryNumber[i])

{

binaryNumber[i] = static_cast<int>(binaryNumber[i]) -48;

++i;

}

--i;

while(i-2>=0){

temp = binaryNumber[i-3] * 8 + binaryNumber[i-2] * 4 + binaryNumber[i-1] * 2 + binaryNumber[i] ;

if(temp > 9)

hexaDecimal[j++] = temp + 55;

else

hexaDecimal[j++] = temp + 48;

i=i-4;

}

if(i ==1)

hexaDecimal[j] = binaryNumber[i-1] * 2 + binaryNumber[i] + 48 ;

else if(i==0)

hexaDecimal[j] = binaryNumber[i] + 48 ;

else

--j;

cout << "Equivalent hexadecimal value: ";

while(j>=0){

cout << hexaDecimal[j--];

}

#define MAX 1000

int main(){

char binaryNumber[MAX],hexaDecimal[MAX];

int temp;

long int i=0,j=0;

cout << "Enter any number any binary number: ";

cin >> binaryNumber;

while(binaryNumber[i])

{

binaryNumber[i] = static_cast<int>(binaryNumber[i]) -48;

++i;

}

--i;

while(i-2>=0){

temp = binaryNumber[i-3] * 8 + binaryNumber[i-2] * 4 + binaryNumber[i-1] * 2 + binaryNumber[i] ;

if(temp > 9)

hexaDecimal[j++] = temp + 55;

else

hexaDecimal[j++] = temp + 48;

i=i-4;

}

if(i ==1)

hexaDecimal[j] = binaryNumber[i-1] * 2 + binaryNumber[i] + 48 ;

else if(i==0)

hexaDecimal[j] = binaryNumber[i] + 48 ;

else

--j;

cout << "Equivalent hexadecimal value: ";

while(j>=0){

cout << hexaDecimal[j--];

}

Expert's answer

Learn more about our help with Assignments: C++

## Comments

## Leave a comment