| | |
Real number to binary?
![]() |
•
•
Join Date: Jan 2005
Posts: 7
Reputation:
Solved Threads: 0
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!)
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!)
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <iostream.h> #include <string.h> #include <math.h> void main(void) { float value=0, tempvalue=0; int flag =0; float decright=0; int decleft=0; printf("Enter a real number: "); scanf("%f", &value); if (value < 0) flag = 1; value=tempvalue=fabs(value); decleft=(int)tempvalue; decright=value-decleft; // separate right & left side of decimal // convert left side to decimal to binary int j=0, k = 0, n = 0; int remain; char temp[80]; char binleft[80]; char binright[80]; float track; do { remain = decleft % 2; if (remain == 1) temp[k++] = '1'; else temp[k++] = '0'; decleft = decleft / 2; } while (decleft != 0); while (k >= 0) { binleft[n++] = temp[--k]; // reverse } binleft[n-1] = 0; // add NULL printf("\n The binary value of is %s \n",binleft); // convert right side to binary do { track = decright*2; if ( (track-1)<0 ) { decright = track; binright[j] = '0'; j = ++j; } else { decright = track-1; binright[j] = '1'; j = ++j; } } while (decright > 1); printf("\n binary of right is %s \n", binright); }
Would some adaptations of this be helpful? http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <limits.h> 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; } char *bits_block ( char *dest, const void *object, size_t size ) { char *start = dest; const unsigned char *byte = object; for ( byte += size - 1; size-- > 0; --byte ) { bits_uchar ( dest, *byte ); dest += CHAR_BIT; *dest++ = size > 0 ? '-' : 0; } return start; } int main(void) { char result [ 80 ]; float value = 5.375; printf ( "%g = \"%s\"\n", value, bits_block ( result, &value, sizeof value ) ); return 0; } /* my output 5.375 = "01000000-10101100-00000000-00000000" */
"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
•
•
Join Date: Jan 2005
Posts: 7
Reputation:
Solved Threads: 0
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;
}
•
•
•
•
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??
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
![]() |
Similar Threads
- PLZ Some one help me , How i can convert a HEXA number to binary number using emu8086 (Assembly)
- Assembly couting number of 1s in binary number (Assembly)
- Display a number in binary (C#)
- Binary Real to Decimal Algorithm (Python)
Other Threads in the C Forum
- Previous Thread: two elements in 2d char array stick together, whY!!??
- Next Thread: Urgent help needed.!!.Please
| Thread Tools | Search this Thread |
* adobe ansi api array arrays binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking hardware highest homework i/o inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h






