C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 3rd, 2012, 10:56 AM
jaggu jaggu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 2 jaggu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #2  
Old December 3rd, 2012, 01:59 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,249 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 17 h 27 m 51 sec
Reputation Power: 1985
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.

Reply With Quote
  #3  
Old December 3rd, 2012, 02:47 PM
jaggu jaggu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 2 jaggu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Code working for Hex, but not for Binary

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap