# HExadecimal to Binary... plEase check d problem with This program....

Discussion in 'C' started by Shantanu_Dutta, Oct 12, 2009.

1. ### Shantanu_DuttaNew Member

Joined:
Oct 12, 2009
Messages:
1
Likes Received:
0
Trophy Points:
0
/* Program to convert hexAdeCimal into binary form........ Q_13......... */

Code:
```   #include<stdio.h>
#include<conio.h>
#include<math.h>

int *input( int *l_pos )
{
*l_pos = 0 ;
int i = 0 , j;
int t_bit , *arr ;
printf("\n\n\tEnter the bits in hexadecimal form( 0 to 9 and A to F in CAPS ) in ") ;
printf("\n\tsequence and to quit entering , press 'RETURN' key ........ ");

printf("\n\n\tStart entering bits .............  \n\n\t");

while( ( t_bit = getche() ) != 13 )
{
*( arr + i ) = t_bit ;
*l_pos = i ;
++i ;
printf("  ");
}

return arr ;
}

int *dec_calc( int bit )
{
int j , *arr ;
bit = bit - 48 ;

for( j = 0 ; j < 4 ; j++ )
{
*( arr + j ) = 0 ;
}
j-- ;

while( bit != 0 )
{
*( arr + j ) = bit % 2 ;
j-- ;
bit /= 2 ;
}

return arr ;
}

int *alfa_calc( int bit )
{
int j = 0 , *arr ;

switch( bit )
{
case 65 : bit = 10 ; break ;
case 66 : bit = 11 ; break ;
case 67 : bit = 12 ; break ;
case 68 : bit = 13 ; break ;
case 69 : bit = 14 ; break ;
case 70 : bit = 15 ; break ;
}

for( j = 0 ; j < 4 ; j++ )
{
*( arr + j ) = 0 ;
}
j-- ;

while( bit != 0 )
{
*( arr + j ) = bit % 2 ;
j-- ;
bit /= 2 ;
}

return arr ;
}

void main()
{
clrscr();
int *arr , *f_arr ;
int l_pos , f_lpos = 0 , pos = 0 , i = 0 , j , *bin_diz , chk = 0 ;

arr = input( &l_pos ) ;

while( i <= l_pos )
{

for( j = 48 ; j <= 57 ; j++ )
{
if( *( arr + i ) == j )
{
bin_diz = dec_calc( *( arr + i ) ) ;

for( pos = 0 ; pos < 4 ; pos++ )
{
*( f_arr + f_lpos ) = *( bin_diz + pos ) ;
++f_lpos ;
}
pos = 0 ;
chk = 1 ;
break ;
}
}

if( !chk )
{
for( j = 65 ; j <= 70 ; j++ )
{
if( *( arr + i ) == j )
{
bin_diz = alfa_calc( *( arr + i ) ) ;
while( pos < 4 )
{
*( f_arr + f_lpos ) = *( bin_diz + pos ) ;
++f_lpos , ++pos ;
}
pos = 0 ;
chk = 1 ;
break ;
}
}

if( !chk )
{
for( j = 71 ; j <= 90 ; j++ )
{
if( *( arr + i ) == j )
{
printf("\n\n\tHexadecimal bits entered are not valid ! ");
chk = 1 ;
break ;
}
}

if( !chk )
{
for( j = 97 ; j <= 122 ; j++ )
{
if( *( arr + i ) == j )
{
printf("\n\n\tHexadecimal bits entered are not valid ! ");
chk = 1 ;
break ;
}
}

if( !chk )
{
printf("\n\n\tHexadecimal bits entered are not valid ! ");
chk = 1 ;
break ;
}

}
}
}

chk = 0 ;
i++ ;

}

printf("\n\n\n\tAfter conversion , number in binary form is : ' " );

for( i = 0 ; i < f_lpos  ; i++ )
{
printf("%d" , *( f_arr + i ) );
}
printf(" '");

getch();

}```

Last edited by a moderator: Oct 13, 2009
2. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
Likes Received:
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
Well, we can't read your mind. What does it do wrong? Does it build? If not what are the errors? If it builds, what does it do when you run it? What line of code goes wrong? If it displayed the wrong output, what output did you get, and what did you expect? It looks like it might take some input from the user, so what input did you give it?