473,426 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,426 software developers and data experts.

how to convert decimal number to binary

1
a program that takes a decimal number input from the user and displays its binary number equivalent in 12 bits on the screen.

For example:
Enter a decimal number: 32
Its Binary Equivalent is : 000000100000
Oct 30 '06 #1
32 46115
a program that takes a decimal number input from the user and displays its binary number equivalent in 12 bits on the screen.

For example:
Enter a decimal number: 32
Its Binary Equivalent is : 000000100000

Do all process through recursive function....and print the values in the main function....
Oct 30 '06 #2
vninja
40
uhhhh stay away from recursive programming!!!!! yea its easy but it eats up mem and cpu usage!!!!!
i.e.
go to a porn site (and turn your popup blocker off!!!).
this is what recursive programing does if each window was a recursive function call.

pretty much each time you call a function (lets say b) the calling function (lets say a) is put on hold.
therefor function a calls b and both are open until b closes/ends so that function a may continue. with recursion a calls a so a is put on hold while another instance of a is running. do this enough times and your comp will crash (well might as well).

i'd suggest a loop for this. something along the lines of

removed as per posting guidlines


the a%2 gives the remainder of number divided by 2.
num=b/2 decreases the number (moves over 1 bit) until num = 1 and the loop ends. this SHOULD give you a bin representation of the number. however its not going to be with 12 bits. try n figure out the rest if need furthur help post it up! gl
Oct 30 '06 #3
removed as per posting guidlines


BOOYAH!!!!!111111 I AM TEH_MASTA1!11
May 17 '07 #4
AdrianH
1,251 Expert 1GB
removed as per posting guidlines


BOOYAH!!!!!111111 I AM TEH_MASTA1!11
Who apperently can't spell. ;) :D


Adrian
May 17 '07 #5
Ganon11
3,652 Expert 2GB
uhhhh stay away from recursive programming!!!!! yea its easy but it eats up mem and cpu usage!!!!!
There are some problems that are just easier, conceptually and memory-wise, to solve with recursion. One example is traversing a binary tree - it's possible using loops, but I don't want to deal with all of that.
May 17 '07 #6
AdrianH
1,251 Expert 1GB
There are some problems that are just easier, conceptually and memory-wise, to solve with recursion. One example is traversing a binary tree - it's possible using loops, but I don't want to deal with all of that.
Further to what Ganon is saying, compiler optimisers nowadays can do some unrolling of recursion functions.


Adrian
May 17 '07 #7
Again, code removed as per posting guidelines


BOOYA!!! 1337 PROGRAM RIGHT HERE. I spell fine.

Adrian.
May 18 '07 #8
AdrianH
1,251 Expert 1GB
Again, code removed as per posting guidelines


BOOYA!!! 1337 PROGRAM RIGHT HERE. I spell fine.

Adrian.
ROFLMAO L7


Adrian
May 18 '07 #9
removed as per posting guidelines


Soak it in boys. Lookin's for free, touchin's gonna cost ya.
Copy & Paste this for TEH_1337 HAX!!!!

Adrian. are you drunk?
// Nope but this oughta do it.
May 22 '07 #10
AdrianH
1,251 Expert 1GB
removed as per posting guidelines


Soak it in boys. Lookin's for free, touchin's gonna cost ya.
Copy & Paste this for TEH_1337 HAX!!!!

Adrian. are you drunk?
// Nope but this oughta do it.
Whatever, you are banned for a week. Do it again and you will be banned permanently.


Adrian
May 22 '07 #11
Silent1Mezzo
208 100+
Sigh, stupid people....They'll never learn.
May 22 '07 #12
Savage
1,764 Expert 1GB
Sigh, stupid people....They'll never learn.
Don't be rude and don't be a Savage

:)


Savage
May 22 '07 #13
Plater
7,872 Expert 4TB
If the input number is always going to contain the same number of bits, then you could just do an & operation for each bit (or iterate through the bits)
Expand|Select|Wrap|Line Numbers
  1. if (mydecimal&0x0004==0x0004)
  2. {
  3. //print a 1
  4. }
  5. else
  6. {
  7. //print a 0
  8. }
  9.  
  10. if (mydecimal&0x0002==0x0002)
  11. {
  12. //print a 1
  13. }
  14. else
  15. {
  16. //print a 0
  17. }
  18. if (mydecimal&0x0001==0x0001)
  19. {
  20. //print a 1
  21. }
  22. else
  23. {
  24. //print a 0
  25. }
  26.  
May 22 '07 #14
AdrianH
1,251 Expert 1GB
If the input number is always going to contain the same number of bits, then you could just do an & operation for each bit (or iterate through the bits)
Expand|Select|Wrap|Line Numbers
  1. if (mydecimal&0x0004==0x0004)
  2. {
  3. //print a 1
  4. }
  5. else
  6. {
  7. //print a 0
  8. }
  9.  
  10. if (mydecimal&0x0002==0x0002)
  11. {
  12. //print a 1
  13. }
  14. else
  15. {
  16. //print a 0
  17. }
  18. if (mydecimal&0x0001==0x0001)
  19. {
  20. //print a 1
  21. }
  22. else
  23. {
  24. //print a 0
  25. }
  26.  
Two problems with this. 1. prints out the digits backwards. 2. used copy coding methods which are prone to error.

Other than that, it great. ;)


Adrian
May 23 '07 #15
Plater
7,872 Expert 4TB
hehe actually if you look (and if I had added in a section for every bit) they would print out in the correct order.

And I never claimed to be good at c/c++, just saying it didn't look like anyone had given a solid answer to the post
May 23 '07 #16
bitset<length> myBinary ((long) <your number>) ;

cout<<myBinary;
May 23 '07 #17
AdrianH
1,251 Expert 1GB
bitset<length> myBinary ((long) <your number>) ;

cout<<myBinary;
Hey, didn't know that one. Learn something every day. ;)


Adrian
May 23 '07 #18
Savage
1,764 Expert 1GB
Hey, didn't know that one. Learn something every day. ;)


Adrian
I suppose that we exist to learn.

;)

Savage
May 23 '07 #19
bitset<length> myBinary ((long) <your number>) ;

cout<<myBinary;
GREAT: Two questions.
code for binary/hexadecimal/decimal <-->binary/hexadecimal/decimal.
Jul 11 '07 #20
bitset<length> myBinary ((long) <your number>) ;

cout<<myBinary;
GREAT: Two questions.
First, the code for binary/hexadecimal/decimal <-->binary/hexadecimal/decimal.

Second, is there a reference text/site which has indexed code snippets for math/engineering C++.

TIA

WestCoast101
Jul 11 '07 #21
Plater
7,872 Expert 4TB
GREAT: Two questions.
First, the code for binary/hexadecimal/decimal <-->binary/hexadecimal/decimal.

Second, is there a reference text/site which has indexed code snippets for math/engineering C++.

TIA

WestCoast101
The binary conversions should be above.
Hex is simple.
.ToString("X") on any integer will give you it's value as a hex string.
Int.Parse(string); is overloaded with a 2nd value stateing if it should accept a hex string or not.
Jul 12 '07 #22
weaknessforcats
9,208 Expert Mod 8TB
The binary conversions should be above.
Hex is simple.
.ToString("X") on any integer will give you it's value as a hex string.
Int.Parse(string); is overloaded with a 2nd value stateing if it should accept a hex string or not.
Too bad this isn't C++.
Jul 12 '07 #23
Plater
7,872 Expert 4TB
Too bad this isn't C++.
Ooops, didn't notice what forum I was in. (got this outa control pannel)

I am "borrowing" this code from an embeded development helpfile and modifying it. My C is a little rusty
Expand|Select|Wrap|Line Numbers
  1. BYTE FromHexHelper(char digit) 
  2. {
  3.    if(digit<='9')
  4.      return(digit-'0');
  5.    else
  6.      return((toupper(digit)-'A')+10);
  7. }
  8.  
  9. BYTE FromHex(char[2] hexdigit) 
  10. {
  11.    int lo,hi;
  12.  
  13.    hi = FromHexHelper(hexdigit[0]);
  14.    lo = FromHexHelper(hexdigit[1]);
  15.    if(lo==0xdd)
  16.      return(hi);
  17.    else
  18.      return( hi*16+lo );
  19. }
  20.  
  21. //pass in a char[2] (or bigger) array to hexdigit
  22. void ToHex(byte val, char *hexdigit)
  23. {
  24.    byte hi=0x00;
  25.    byte lo=0x00;
  26.  
  27.    hi=0xF0&val;
  28.    lo=0x0F&val;
  29.    //you could do a switch statement on hi and lo to determine 0-9A-F
  30.    //then populate hexdigit[0] and hexdigit[1]
  31. }
  32.  
Jul 12 '07 #24
a program that takes a decimal number input from the user and displays its binary number equivalent in 12 bits on the screen.

For example:
Enter a decimal number: 32
Its Binary Equivalent is : 000000100000
hi,
i am new to the forum can you suggest me how to send my queries and how to view the replies to my queries?
Jul 20 '07 #25
Meetee
931 Expert Mod 512MB
hi,
i am new to the forum can you suggest me how to send my queries and how to view the replies to my queries?
Hi,

You kindly read Posting Guidelines

Regards
Jul 20 '07 #26
< spoonfeeding code deleted >
Dec 2 '08 #27
donbock
2,426 Expert 2GB
You could repeatedly extract one nibble at a time from your value, using that nibble to index into an array of strings. The binary-string array contains 4-character strings such as "0000", "0001", etc. The hex-string array contains 1-character strings such as "0", "1", etc. Each such array has 16 entries.

How many nibbles are in the value? You either have to have secret knowledge of your compiler that you hard-code into your software or perhaps you could look at <limits.h> to see if your program can adapt itself to the compiler.
Dec 2 '08 #28
weaknessforcats
9,208 Expert Mod 8TB
No one has mentioned an algorithm to solve this.

To check that a bit is set in a binary column, all you do is:

(32/32) = 1 and 1%2 is 1 the 32-bit is set
(32/16) = 2 and 2 % 2 is 0 the 16-bit is not set
etc...
Dec 3 '08 #29
MrPickle
100 100+
@weaknessforcats
If you're using STL bitsets you can do myBitset.test(position);
Dec 3 '08 #30
vmpstr
63
I prefer looping over the number of bits and doing a check along the lines of
Expand|Select|Wrap|Line Numbers
  1. if ( num & (1 << curbit) )
  2.  
If that is true, then bit curbit is set (the least significant bit being bit 0). This is very similar to what weaknessforcats said, except that one is

Expand|Select|Wrap|Line Numbers
  1. if ( (num >> curbit) & 1 )
  2.  
:D
Dec 3 '08 #31
YarrOfDoom
1,247 Expert 1GB
I believe itoa() with base 2 would be kinda spoiling the fun.
That, and you don't learn anything from it.
Dec 3 '08 #32
Here,a much easier way to convert decimal in to base-2,8,16-
Expand|Select|Wrap|Line Numbers
  1. // this program converts the given decimal number into to the desired bases.
  2. #include<iostream.h>
  3. #include<conio.h>
  4. #include<math.h>
  5.  
  6. void main()
  7. {
  8.  clrscr();
  9.  int i, a[1], b, n;
  10.  char ch;
  11.  do
  12.  {
  13.   cout<<"Enter the Decimal Number to be converted: \n";
  14.   cin>>n;
  15.   if(n<0)
  16.    {
  17.      cout<<"Enter a Positive Number: \n";
  18.    }
  19.   else
  20.    {
  21.      cout<<"Enter the Base(2, 8 or 16): \n";
  22.      cin>>b;
  23.      if(b!=2 && b!=8 && b!=16)
  24.             {
  25.             cout<<"Invalid Base!"<<endl;
  26.             }
  27.             else
  28.             {
  29.             i=7;
  30.                 do
  31.                 {
  32.                  a[i]=n%b;
  33.                  n=n/b;
  34.                  i--;
  35.                 }
  36.                 while(n>0);
  37.                 while(i>=0)
  38.                 {
  39.                  a[i]=0;
  40.                  i--;
  41.                 }
  42.                 for(i=0;i<=7;i++)
  43.                 {
  44.                  if (a[i]==10)
  45.                  cout<<"A";
  46.                  if (a[i]==11)
  47.                  cout<<"B";
  48.                  if (a[i]==12)
  49.                  cout<<"C";
  50.                  if (a[i]==13)
  51.                  cout<<"D";
  52.                  if (a[i]==14)
  53.                  cout<<"E";
  54.                  if (a[i]==15)
  55.                  cout<<"F";
  56.                  else
  57.                  cout<<"\t"<<a[i];
  58.                 }
  59.             };
  60.    }
  61.   cout<<"\nDo you wish to convert another number? (y/n)"<<endl;
  62.   cin>>ch;
  63.  }
  64.  while(ch!='n');
  65.  getch();
  66. }
Aug 7 '10 #33

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
1
by: ferran | last post by:
Hi, does anybody know how to convert in C++ from base 10 to any other base without loosing the decimal part of the actual value? I came up with this algorithm to convert from decimal to any base...
7
by: Philipp H. Mohr | last post by:
Hello, I am trying to xor the byte representation of every char in a string with its predecessor. But I don't know how to convert a char into its byte representation. This is to calculate the...
13
by: Hako | last post by:
I try this command: >>> import string >>> string.atoi('78',16) 120 this is 120 not 4E. Someone can tell me how to convert a decimal number to hex number? Can print A, B, C,DEF. Thank you.
7
by: Golan | last post by:
Hi, I need to convert a Binary value to Decimal. I've been told that the value is an unsigned one. How can I do this? I use memcpy into an unsigned char variable, but when I print the value I got...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
9
by: Leo jay | last post by:
i'd like to implement a class template to convert binary numbers to decimal at compile time. and my test cases are: BOOST_STATIC_ASSERT((bin<1111,1111,1111,1111>::value == 65535));...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.