LinuxQuestions.org Decimal to binary
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

 02-01-2005, 01:39 PM #1 maldini1010 Member   Registered: Sep 2004 Location: Montreal Posts: 53 Rep: Decimal to binary Hi, Does anyone know of a function which would convert a decimal number into its equivalent binary(8bits) string. I was on windows and was using itoa, however this functin is no where to be found on my linux system. Thanks maldini
 02-01-2005, 01:53 PM #2 itsme86 Senior Member   Registered: Jan 2004 Location: Oregon, USA Distribution: Slackware Posts: 1,246 Rep: Code: ```{ int num = 86; char str[50]; sprintf(str, "%d", num); }```
 02-01-2005, 01:59 PM #3 maldini1010 Member   Registered: Sep 2004 Location: Montreal Posts: 53 Original Poster Rep: Hi, I just tried the piece of code you sent me and what this code seems to do, is to convert the interger into a string. what i need is something that you pass it the number 7 for example and it will return you 00000111. Let me know what you think Thanks for the help
 02-01-2005, 02:13 PM #4 itsme86 Senior Member   Registered: Jan 2004 Location: Oregon, USA Distribution: Slackware Posts: 1,246 Rep: I didn't realize itoa() did that for you. It wouldn't be hard to create a function to do that...give me a sec.
 02-01-2005, 02:18 PM #5 itsme86 Senior Member   Registered: Jan 2004 Location: Oregon, USA Distribution: Slackware Posts: 1,246 Rep: Here you go: Code: ```itsme@dreams:~/C\$ cat ctob.c #include char *ctob(unsigned char c) { static char buf[9]; int i; for(i = 0;i < 8;++i) buf[i] = (c & (1 << (7 - i))) ? '1' : '0'; buf[8] = '\0'; return buf; } int main(void) { char c = 86; printf("%s\n", ctob(c)); return 0; }``` Code: ```itsme@dreams:~/C\$ ./ctob 01010110``` Last edited by itsme86; 02-01-2005 at 02:21 PM.
 02-01-2005, 02:23 PM #6 jim mcnamara Member   Registered: May 2002 Posts: 964 Rep: try something like this Code: ```#include void printBinary(unsigned int input) { int x=sizeof(unsigned int); x*=CHAR_BIT; for(--x; x>=0; --x) { fprintf(stdout,"%d", !!((unsigned int)1<
 02-01-2005, 04:03 PM #7 maldini1010 Member   Registered: Sep 2004 Location: Montreal Posts: 53 Original Poster Rep: thanks guys, I was sure there was a function part of a linux library which did this. I guess some things are better done from scratch. Thanks a lot guys. maldini

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post nadroj Programming 6 07-12-2005 11:03 AM ipreuss Linux - General 2 03-25-2005 12:13 PM a1ex_007 Programming 6 03-11-2005 09:05 AM acid_kewpie Programming 10 08-20-2003 02:29 PM miguetoo Programming 13 04-24-2003 09:49 PM

LinuxQuestions.org

All times are GMT -5. The time now is 11:47 PM.

 Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -