FOR FREE CONTENT

Binary to HexaDecimal

Back to Programming

Description

The program is written here to convert a binary number into its equivalent hexadecimal number.

The binary number is taken as input from the user. The decimal number can be calculated by adding the products of each digit and the power of 2 starting from 0 from the right to left.

For example, the binary number is 101101.

The decimal number will be:

1*25+0*24+1*23+1*22+0*21+1*21= 32 + 0 + 8 + 4 + 0 + 1 = 45.

Now this decimal value will be converted to its equivalent hexadecimal by dividing the number with 16, again divide the quotient with 16 and so on until the number becomes 0. The remainder after each division will be shown in the reversed order. Hence, we will get the hexadecimal number. The example is given below:

Therefore, the equivalent hexadecimal number is (2D)16

Algorithm

INPUT: A binary number

OUTPUT: Equivalent Hexadecimal Number

PROCESS:

Step 1: [Taking the input]

               Read n [the binary number]

Step 2: [Converting from binary to hexadecimal]

               Set d<-0

               Set p<-0

               [Converting from binary to decimal]

               While n>0 repeat

                              [Calculating the power of 2 and multiplied with the binary digit]

                              Set d<-d+(n mod 10)×2p

                              [Increasing the power]

                              Set p<-p+1

                              Set n<-n/10

               [End of ‘while’ loop]

               [Converting from decimal to hexadecimal]

While d>0 repeat

                        Set r<- d mod 16

                        If r≤9 then

                                    Concatenate the value of variable ‘r’ with ‘s’ in reversed order.

                        Else if r=10 then

                                    Concatenate ‘A’ with ‘s’ in reversed order.

                        Else if r=11 then

                                    Concatenate ‘B’ with ‘s’ in reversed order.

                        Else if r=12 then

                                    Concatenate ‘C’ with ‘s’ in reversed order.

                        Else if r=13 then

                                    Concatenate ‘D’ with ‘s’ in reversed order.

                        Else if r=14 then

                                    Concatenate ‘E’ with ‘s’ in reversed order.

                        Else if r=15 then

                                    Concatenate ‘F’ with ‘s’ in reversed order.

                        Set d<-d/16

            [End of ‘while’ loop]

            [Printing the hexadecimal number]

            Print "The hexadecimal number is: s”

Step 3: Stop.

Code

TIME COMPLEXITY:

while(n>0)--------------------------------------------------O(k)

               {

                              //calculating the power of 2 and multiplied

                              //with the binary digit

                              d=d+(n%10)*pow(2,p);

                              //increasing the power

                              p+=1;

                              n=n/10;

               }

               //converting from decimal to hexadecimal

               p=0;

               while(d>0)-----------------------------------------------O(m)

               {

                              r=d%16;

                              if(r<=9)

                                             hex[p++]=(char)(r+48);

                              else if(r==10)

                                             hex[p++]='A';

                              else if(r==11)

                                             hex[p++]='B';

                              else if(r==12)

                                             hex[p++]='C';

                              else if(r==13)

                                             hex[p++]='D';

                              else if(r==14)

                                             hex[p++]='E';

                              else if(r==15)

                                             hex[p++]='F';

                              d=d/16;

               }

The time complexity of this program is O(k+m) where ‘k’ is the number of digits of the binary number and ‘m’ is the number of digits of the decimal number.

SPACE COMPLEXITY:

The space complexity of this program is O(1) as it requires a constant number of memory spaces to execute the program.