The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Code working for Hex, but not for Binary
Discuss Code working for Hex, but not for Binary in the C Programming forum on Dev Shed. Code working for Hex, but not for Binary 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.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 3rd, 2012, 10:56 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 2
Time spent in forums: 38 m 52 sec
Reputation Power: 0
|
|
|
Code working for Hex, but not for Binary
Hi,
I have two functions. One converts ascii to hex and the other uses the function and displays the inverted data. I have shown the two functions below
Code:
/*----------------------*/
/* Convert ASCII to hex */
/*----------------------*/
unsigned int asc2hex(char *str)
{
unsigned int res;
char *p;
res = 0;
for (p = str; *p != '\0' ; ++p)
{
res <<= 4;
if (*p >= '0' && *p <= '9')
res |= *p - '0';
else
res |= *p - 'A' + 10;
}
return res;
}
Code:
/*----------------------*/
/* Invert data */
/*----------------------*/
invdata( int cycle, char *actstr, char * userData)
{
char this_input_data[1024];
unsigned int val;
get_facvalue(userData, cycle, this_input_data);
val = asc2hex(this_input_data);
sprintf(actstr,"%08u",~val);
return 0;
}
The above functions work perfectly well. But, when I try to do the same and display the result in binary I don't get the proper result. The result just shows "b". I have shown the two functions below
Code:
/*----------------------*/
/* Convert ascii to binary*/
/*----------------------*/
unsigned int asc2bin(char *str)
{
unsigned int res;
unsigned int binres;
char *p;
binres = 0;
for (p = str; *p != '\0' ; ++p)
{
if (*p >= '0' && *p <= '9')
res = *p - '0';
else
res = *p - 'A' + 10;
binres <<= 4;
switch(res)
{
case '0': binres|='0000';break; case '1': binres|='0001';break; case '2': binres|='0010';break; case '3': binres|='0011';break; case '4': binres|='0100';break; case '5': binres|='0101';break; case '6': binres|='0110';break; case '7': binres|='0111';break; case '8': binres|='1000';break; case '9': binres|='1001';break; case 'A': binres|='1010';break; case 'B': binres|='1011';break; case 'C': binres|='1100';break; case 'D': binres|='1101';break; case 'E': binres|='1110';break; case 'F': binres|='1111';break; case 'a': binres|='1010';break; case 'b': binres|='1011';break; case 'c': binres|='1100';break; case 'd': binres|='1101';break; case 'e': binres|='1110';break; case 'f': binres|='1111';break; default:break;
}
}
return binres;
}
Code:
/*----------------------*/
/* Invert data */
/*----------------------*/
invdata( int cycle, char *actstr, char * userData)
{
char this_input_data[1024];
unsigned int val;
get_facvalue(userData, cycle, this_input_data);
val = asc2bin(this_input_data);
sprintf(actstr,"%032b",~val);
return 0;
}
Can you tell me what is wrong?
Thanks!
|

December 3rd, 2012, 01:59 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
There is no "%b" format specifier. Didn't you ever read the help file or man page on printf?
Also, why do you think that "%u" worked for hex? That would output an unsigned decimal, not hexadecimal. You need an x for hex. Again, Read the Manual! (RTFM)
And just why do you refuse to format your code? The entire idea of code tags is to preserve your code's formatting, so you refusing to format your code you are defeating the purpose of code tags. And your formatting of the cases of a switch statement far beyond lame (you very nearly drove me to use a very non-politically-correct term to describe it). This is how it should have looked:
Code:
/*----------------------*/
/* Convert ascii to binary*/
/*----------------------*/
unsigned int asc2bin(char *str)
{
unsigned int res;
unsigned int binres;
char *p;
binres = 0;
for (p = str; *p != '\0' ; ++p)
{
if (*p >= '0' && *p <= '9')
res = *p - '0';
else
res = *p - 'A' + 10;
binres <<= 4;
switch(res)
{
case '0': binres|='0000'; break;
case '1': binres|='0001'; break;
case '2': binres|='0010'; break;
case '3': binres|='0011'; break;
case '4': binres|='0100'; break;
case '5': binres|='0101'; break;
case '6': binres|='0110'; break;
case '7': binres|='0111'; break;
case '8': binres|='1000'; break;
case '9': binres|='1001'; break;
case 'A': binres|='1010'; break;
case 'B': binres|='1011'; break;
case 'C': binres|='1100'; break;
case 'D': binres|='1101'; break;
case 'E': binres|='1110'; break;
case 'F': binres|='1111'; break;
case 'a': binres|='1010'; break;
case 'b': binres|='1011'; break;
case 'c': binres|='1100'; break;
case 'd': binres|='1101'; break;
case 'e': binres|='1110'; break;
case 'f': binres|='1111'; break;
default:
break;
}
}
return binres;
}
The entire idea of formatting your code and using code tags to preserve that formatting is so that it will be readable. Not using code tags removes that readability. Not even formatting makes it unreadable from the start. Pulling the stupid crap you did with the switch cases goes far beyond that and smacks of a deliberate affront.
Please note that while the code for each case should be broken out into individual lines that are properly indented, this is one of the few cases where keeping the entire case on one line actually improves readability.
|

December 3rd, 2012, 02:47 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 2
Time spent in forums: 38 m 52 sec
Reputation Power: 0
|
|
|
My code is formatted. When I tried to copy and paste is between the code tags it all got pasted on one single line. I tried to reformat it. This is the first time I'm posting something on a forum, so, I'm obviously not going to be perfect at it. Anyway, its pretty clear you were really annoyed so my apologies.
I didn't use %u for the hex value. I was just trying different formats. Forgot to change it back to %x before I posted here.
Thank you for letting me know the %b was my mistake!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|