HEx to Binary... plEase check d problem with This program....
Discuss HEx to Binary... plEase check d problem with This program.... in the C Programming forum on Dev Shed. HEx to Binary... plEase check d problem with This program.... C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
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 ........ ");
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 ;
}
Posts: 6,256
Time spent in forums: 2 Months 2 Weeks 5 Days 20 h 36 m 23 sec
Reputation Power: 1985
Your primary problem is that your code is totally unreadable. Use code tags to preserve formatting:
Place [code] in front of your code
and [/code] after your code.
Eg:
Code:
/* Program to convert hexAdeCimal into binary form........ Q_13......... */
#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();
}
Now, just what problem is it that you are having with this code? First you give us an unreadable listing and then you make us play guessing games with you. That kind of nonsense will only earn you severe flamings. Be nice with us and we will be nice with you and even help you. Play stupid tricks on us and we will have no reason to be nice with you.
If you are having a problem, then you must tell us what that problem is.
Does it not compile? If not, then what compiler error messages do you get?
Does it compile and crash when you run it? Then you must tell us that fact, as insignificant as you might feel that it is. If it only crashes after you have provided certain input (eg, runs fine until you have entered all the inputs it asks for), then you need to tell us that as well.
Does it compile and run, but give you unexpected results? Then you must tell us what results you had expected and what you got instead. Explicitly.
Most of us have our day jobs and little spare time to help out here. If you just want to waste our time, we won't. If you really do want help with something, then you must help us to help you. Capice?
I will hazard a guess that your program crashes. I noticed that in most of your functions you make this declaration: int *arr ;
and then later in the function you make an assignment such as: *( arr + j ) = 0 ;
without ever having assigned any value to the pointer, arr, thus making it an uninitialized pointer.
Since arr is a local variable, its contents are initially pure garbage; ie, whatever just happens to have been last written to that location -- "garbage" is the actual term used. Which means that if you try to use that garbage as a pointer value with which to write to a location in memory, then you could be trying to write to anywhere on the system. Since your program does not have access to the vast majority of memory locations on your system, your program should crash due either to an access violation or a segmentation fault, depending on your OS.
Never attempt to use an uninitialized pointer. For that matter, did your compiler issue any warnings? Some compilers will try to warn you about using an uninitialized variable. Treat your compiler's warnings as if they were error messages. A warning usually indicates that you've written something that's confusing it; a confused compiler can be a dangerous thing.
PS
Just what is this program trying to do? Does it accept an input string that represents a hex value and print an output string that represents the equivalent binary value?
Your code looks far too cumbersome and overly complex for performing such a simple task. Converting between hex and binary is so simple that a human can perform it on sight, unlike converting between either number base and decimal which does require some hand calculations. Why else do you think that octal and hex are so popular? Write out all 16 hex digits and their binary equivalents and think about it.
An explanation of exactly what you're trying to do and how (ie, comment your code so that it explains what that code is doing) would help out immensely.
PPS
You might want to make arr an parameter of the functions that the calling function will pass in. Since it's a pointer, any changes you make within the function to what it points to will be remembered outside the function. Then you must be sure that the calling function passes an actual existing array to those functions it calls -- HINT: main does not declare an actual arr array either.
Last edited by dwise1_aol : October 12th, 2009 at 03:22 PM.
Posts: 4
Time spent in forums: 18 m 34 sec
Reputation Power: 0
hexadecimal to Binary...............
fIrst of all M xtremely sorry for not mentioning wHat I wanted to ask.................
The main problem with the program is it does not give the desired output..........
eg.
if input is 2C6B............ it will simply convert 2 to 0010 and will exit the main loop instead of continuing the loop...............
that is, instead of giving an output for 2C6B as 0010110001101011............ it simply gives 0010 with an additional statemnt "Null pointer assignment"...............
I am executing the program in Turbo C++ compiler............
Now if I trace the program by pressing 'F7'....... it simply does whatever the program is meant to do......... according to logic..........
2)........ Y dis program is cumbersome because doing the program using a list of switch cases is too basic..............
what i want to do here is........ it will take the input of the hexadecimal bits in its ASCII format........
eg. for 2C6B.... it wud be 50 67 54 66................... then it will simply compare where does the range falls for each bit ( for alphabet and integer ( 0-9 and A-F ) and will convert each bit to its binary form.
the problem lies now. once it converts, it will store in a temporary unassigned array of the respective functions after which it will return the function to the main where it will transfer the bits contained in the arr which is now in bin_diz to f_arr................
once it completes this procedure for each bit..... it will simply print f_arr... which wud be its answer..............
But, the main issue here is though it does d process , but only once..... i.e. for the very first bit................ there r no errors also...........
so i m completely confused of what problem could the program have..............
Hope i was able to give a fair idea bout mY program and you could help me solve my problem.............
int *input( int *l_pos ) // function to input hex bits into a temorary array "arr"
{
*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 ........ ");
while( bit != 0 )
{
*( arr + j ) = bit % 2 ;
j-- ;
bit /= 2 ;
}
return arr ;
}
int *alfa_calc( int bit ) // function to convert bits (A-F) to its equivalent binary form and store it in arr
{
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 ;
}
Posts: 4
Time spent in forums: 18 m 34 sec
Reputation Power: 0
Code:
/* Program to convert hexAdeCimal into binary form........ Q_13......... */
#include<stdio.h>
#include<conio.h>
#include<math.h>
int *input( int *l_pos ) // function to input hex bits into a temorary array "arr"
{
*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 ) // function to convert bits (0-9) to its equivalent binary form and store it in arr
{
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 ) // function to convert bits (A-F) to its equivalent binary form and store it in arr
{
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 ) ; // input function called and the array stored in arr..........
while( i <= l_pos )
{
for( j = 48 ; j <= 57 ; j++ )
{
if( *( arr + i ) == j )
{
bin_diz = dec_calc( *( arr + i ) ) ; // dec_calc() func called and the values stored in bin_diz
for( pos = 0 ; pos < 4 ; pos++ ) // transfering elements from bin_diz to f_arr which is the final array......
{
*( 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 ) ) ; // alga_calc() func called and the values stored in bin_diz
while( pos < 4 ) // transfering elements from bin_diz to f_arr which is the final array......
{
*( f_arr + f_lpos ) = *( bin_diz + pos ) ;
++f_lpos , ++pos ;
}
pos = 0 ;
chk = 1 ;
break ;
}
}
if( !chk ) // error checkin ( if bits betwn ( G -Z ) )
{
for( j = 71 ; j <= 90 ; j++ )
{
if( *( arr + i ) == j )
{
printf("\n\n\tHexadecimal bits entered are not valid ! ");
chk = 1 ;
break ;
}
}
if( !chk ) // error checkin ( if bits betwn ( a - z ) )
{
for( j = 97 ; j <= 122 ; j++ )
{
if( *( arr + i ) == j )
{
printf("\n\n\tHexadecimal bits entered are not valid ! ");
chk = 1 ;
break ;
}
}
if( !chk ) // error checkin ( if any other possibilities)
{
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();
}
fIrst of all M xtremely sorry for not mentioning wHat I wanted to ask.................
The main problem with the program is it does not give the desired output..........
eg.
if input is 2C6B............ it will simply convert 2 to 0010 and will exit the main loop instead of continuing the loop...............
that is, instead of giving an output for 2C6B as 0010110001101011............ it simply gives 0010 with an additional statemnt "Null pointer assignment"...............
I am executing the program in Turbo C++ compiler............
Now if I trace the program by pressing 'F7'....... it simply does whatever the program is meant to do......... according to logic..........
2)........ Y dis program is cumbersome because doing the program using a list of switch cases is too basic..............
what i want to do here is........ it will take the input of the hexadecimal bits in its ASCII format........
eg. for 2C6B.... it wud be 50 67 54 66................... then it will simply compare where does the range falls for each bit ( for alphabet and integer ( 0-9 and A-F ) and will convert each bit to its binary form.
the problem lies now. once it converts, it will store in a temporary unassigned array of the respective functions after which it will return the function to the main where it will transfer the bits contained in the arr which is now in bin_diz to f_arr................
once it completes this procedure for each bit..... it will simply print f_arr... which wud be its answer..............
But, the main issue here is though it does d process , but only once..... i.e. for the very first bit................ there r no errors also...........
so i m completely confused of what problem could the program have..............
Hope i was able to give a fair idea bout mY program and you could help me solve my problem.............
Posts: 6,256
Time spent in forums: 2 Months 2 Weeks 5 Days 20 h 36 m 23 sec
Reputation Power: 1985
OK, you used code tags, but you posted unformatted code! The reason for using code tags is to preserve your code's formatting. Putting unformatted code in code tags is nothing but malicious compliance. So why are you now deliberately insulting us?
Second, what does arr point to? Tell me specifically what arr points to. In other words, show me explicitly where you assign a value to arr that causes it to actually point to something.
Until you post code that we can read, we can't even begin to try to figure out why it terminates prematurely. I'm immensely surprised that it doesn't crash and barf in your lap. Unless it does, but you just didn't notice. If I can remember that many decades ago, Turbo C++ would report the runtime error that would cause the program to crash.
If the IDE doesn't report that, then open a command prompt (AKA "console", AKA "DOS window") and run the .EXE file from the command line. cmd.exe will report a crash.
PS
I remember that some of Borland's products included a debugger. I forget when the C++ line started including it, so I don't know whether your version has it. You might check it out and give it a try. That way, you can step through your code line-by-line and see exactly what's going on.
Last edited by dwise1_aol : October 12th, 2009 at 09:22 PM.
Posts: 837
Time spent in forums: 1 Week 2 Days 17 h 43 m 22 sec
Reputation Power: 526
My desire to protect an industry already in decline from a further influx of under-qualified entrants (complete with impressive, un-verifiable overseas references) far overwhelms any desire to provide help in this situation.
Posts: 337
Time spent in forums: 5 Days 2 h 15 m 47 sec
Reputation Power: 45
You really have serious problem. Take a look at input() ,dec_calc(),alfa_calc() functions. You are not initializing arr (pointer to integer) . And what's 13 ??