944,098 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 9091
  • C RSS
Jan 28th, 2005
0

Real number to binary?

Expand Post »
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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
akila is offline Offline
7 posts
since Jan 2005
Jan 28th, 2005
0

Re: Real number to binary?

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
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jan 29th, 2005
0

Re: Real number to binary?

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
akila is offline Offline
7 posts
since Jan 2005
Jan 29th, 2005
0

Re: Real number to binary?

nevermind, I figured out the typecast.. silly me. The code is working like a charm, thank you so much!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
akila is offline Offline
7 posts
since Jan 2005
Jan 29th, 2005
0

Re: Real number to binary?

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??

Quote 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;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
akila is offline Offline
7 posts
since Jan 2005
Jan 29th, 2005
0

Re: Real number to binary?

Quote 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.)
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: two elements in 2d char array stick together, whY!!??
Next Thread in C Forum Timeline: Urgent help needed.!!.Please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC