LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-01-2005, 01:39 PM   #1
maldini1010
Member
 
Registered: Sep 2004
Location: Montreal
Posts: 53

Rep: Reputation: 15
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
 
Old 02-01-2005, 01:53 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Code:
{
  int num = 86;
  char str[50];

  sprintf(str, "%d", num);
}
 
Old 02-01-2005, 01:59 PM   #3
maldini1010
Member
 
Registered: Sep 2004
Location: Montreal
Posts: 53

Original Poster
Rep: Reputation: 15
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
 
Old 02-01-2005, 02:13 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
 
Old 02-01-2005, 02:18 PM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Here you go:
Code:
itsme@dreams:~/C$ cat ctob.c
#include <stdio.h>

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.
 
Old 02-01-2005, 02:23 PM   #6
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
try something like this
Code:
#include <limits.h>
void printBinary(unsigned int input)
{
   int x=sizeof(unsigned int);
   x*=CHAR_BIT;
   for(--x; x>=0; --x)
   {
        fprintf(stdout,"%d", !!((unsigned int)1<<x & input) );
   }
}
 
Old 02-01-2005, 04:03 PM   #7
maldini1010
Member
 
Registered: Sep 2004
Location: Montreal
Posts: 53

Original Poster
Rep: Reputation: 15
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to print 2 decimal places of a double? nadroj Programming 6 07-12-2005 11:03 AM
Decimal Symbol - does it exist??? ipreuss Linux - General 2 03-25-2005 12:13 PM
How to use decimal numbers in in shell scripts a1ex_007 Programming 6 03-11-2005 09:05 AM
converting fake hex to decimal in c acid_kewpie Programming 10 08-20-2003 02:29 PM
c++ binary to decimal conversion.. miguetoo Programming 13 04-24-2003 09:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration