Real number to binary?

Reply

Join Date: Jan 2005
Posts: 7
Reputation: akila is an unknown quantity at this point 
Solved Threads: 0
akila akila is offline Offline
Newbie Poster

Real number to binary?

 
0
  #1
Jan 28th, 2005
This may be trivial for most of you, but I've been scratching my head as to how to convert real numbers (ie 5.375) to binary. Actually, I'm trying to convert the real number to 32-bit IEEE binary format, but I'm just working with trying to convert it to binary for now.

I can get the part before the decimal to work out (ie the 5), but I can't seem to get the .375 conversion. Here's what I've come up with so far... any help will be much appreciated!! (horrible code I know, please bear with me!)

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream.h>
  4. #include <string.h>
  5. #include <math.h>
  6.  
  7.  
  8. void main(void)
  9. {
  10. float value=0, tempvalue=0;
  11. int flag =0;
  12. float decright=0;
  13. int decleft=0;
  14.  
  15. printf("Enter a real number: ");
  16. scanf("%f", &value);
  17.  
  18. if (value < 0)
  19. flag = 1;
  20.  
  21. value=tempvalue=fabs(value);
  22.  
  23. decleft=(int)tempvalue;
  24. decright=value-decleft; // separate right & left side of decimal
  25.  
  26. // convert left side to decimal to binary
  27.  
  28. int j=0, k = 0, n = 0;
  29. int remain;
  30. char temp[80];
  31. char binleft[80];
  32. char binright[80];
  33. float track;
  34.  
  35. do
  36. {
  37. remain = decleft % 2;
  38.  
  39. if (remain == 1)
  40. temp[k++] = '1';
  41. else
  42. temp[k++] = '0';
  43.  
  44. decleft = decleft / 2;
  45.  
  46. } while (decleft != 0);
  47.  
  48. while (k >= 0)
  49. {
  50. binleft[n++] = temp[--k]; // reverse
  51. }
  52.  
  53. binleft[n-1] = 0; // add NULL
  54.  
  55. printf("\n The binary value of is %s \n",binleft);
  56.  
  57. // convert right side to binary
  58.  
  59. do
  60. {
  61. track = decright*2;
  62.  
  63. if ( (track-1)<0 )
  64. {
  65. decright = track;
  66. binright[j] = '0';
  67. j = ++j;
  68. }
  69.  
  70. else
  71. {
  72. decright = track-1;
  73. binright[j] = '1';
  74. j = ++j;
  75. }
  76. } while (decright > 1);
  77.  
  78. printf("\n binary of right is %s \n", binright);
  79.  
  80. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Real number to binary?

 
0
  #2
Jan 28th, 2005
Would some adaptations of this be helpful?
  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. char *bits_uchar ( char *dest, unsigned char value )
  5. {
  6. char *start = dest;
  7. unsigned char bit;
  8. for ( bit = 1 << (CHAR_BIT - 1); bit > 0; bit >>= 1 )
  9. {
  10. *dest++ = value & bit ? '1' : '0';
  11. }
  12. *dest = 0;
  13. return start;
  14. }
  15.  
  16. char *bits_block ( char *dest, const void *object, size_t size )
  17. {
  18. char *start = dest;
  19. const unsigned char *byte = object;
  20. for ( byte += size - 1; size-- > 0; --byte )
  21. {
  22. bits_uchar ( dest, *byte );
  23. dest += CHAR_BIT;
  24. *dest++ = size > 0 ? '-' : 0;
  25. }
  26. return start;
  27. }
  28.  
  29. int main(void)
  30. {
  31. char result [ 80 ];
  32. float value = 5.375;
  33. printf ( "%g = \"%s\"\n", value,
  34. bits_block ( result, &value, sizeof value ) );
  35. return 0;
  36. }
  37.  
  38. /* my output
  39. 5.375 = "01000000-10101100-00000000-00000000"
  40. */
http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 7
Reputation: akila is an unknown quantity at this point 
Solved Threads: 0
akila akila is offline Offline
Newbie Poster

Re: Real number to binary?

 
0
  #3
Jan 29th, 2005
When I tried it I get a type cast error ?

Conversion from 'void*' to pointer to non-'void' requires an explicit cast

thanks so much.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 7
Reputation: akila is an unknown quantity at this point 
Solved Threads: 0
akila akila is offline Offline
Newbie Poster

Re: Real number to binary?

 
0
  #4
Jan 29th, 2005
nevermind, I figured out the typecast.. silly me. The code is working like a charm, thank you so much!
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 7
Reputation: akila is an unknown quantity at this point 
Solved Threads: 0
akila akila is offline Offline
Newbie Poster

Re: Real number to binary?

 
0
  #5
Jan 29th, 2005
If it's not too much trouble, could you also explain to me what is going on in your code? I really want to understand this and learn. In particular, just the part quoted below. How does this convert the value to binary??

Originally Posted by Dave Sinkula

char *bits_uchar ( char *dest, unsigned char value )
{
char *start = dest;
unsigned char bit;
for ( bit = 1 << (CHAR_BIT - 1); bit > 0; bit >>= 1 )
{
*dest++ = value & bit ? '1' : '0';
}
*dest = 0;
return start;
}
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Real number to binary?

 
0
  #6
Jan 29th, 2005
Originally Posted by akila
If it's not too much trouble, could you also explain to me what is going on in your code? I really want to understand this and learn. In particular, just the part quoted below. How does this convert the value to binary??
Basically it loops through the bits of the object representation.

Did you look at Part 1 and/or Part 3? If you have specific questions or comments, I can try to improve these snippets. (I'm too familiar with them to remember what needs better explanations.)
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC