#include "stdio.h" #include "stdlib.h" #include "string.h" #define datatype int#define MAX_SIZE 50#define OK 1#define Err 0 /*** Code : By YangLong September 21, 2014,** 0x00: function Description * xd2b Convert any one floating-point number to 2 binary * &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;XD2B (Double f,char* s, bool z) * parameter: The floating-point number that needs to be converted * parameter two: the character array that holds the result * parameter Three: z = true representative Integer conversion * z = false time Indicates floating-point number Convert ** 0x01 ieee754 (char* s) * parameter: convert the character array in &NBSP;XD2B to ieee754 format * 0x02 Insufficient:* results shown as split * to link up with string stitching on line ** 0x03 Pending Optimization: * results displayed as 16 Binary * converting IEEE754 to True ** is not a complicated program, but a bit of a mess to write. */typedef struct stack{ datatype data[max_size]; int top;} Stk;void init (stk* s) { s->top = -1;} Bool push (stk *s,datatype d) { if (s->top+1>max_size-1) { printf (" ' push ' error: overflow!\n "); exit (0); } s->data[++s->top] = d; return ok;} Datatype pop (stk* s) { if (s->top<0) { printf (" ' Pop ' error: overflow!"); exit (0); } datatype data = s->data[s->top--]; return data;} Bool isempty (stk* s) { return s->top == -1 ? ok : err;} VOID&NBSP;XD2B (double f,char* s, bool z) { int i=0,j=0; stk ss; init (&SS); &nbsP;if (f<0) { s[0] = ' 1 '; j = 1; f = f* ( -1.0); } else { s[0] = ' 0 '; j = 1; } int q = (int) F; while (q) { push ( &ss,q%2); q = q/2; } while (! IsEmpty (&SS)) { s[j++] = Pop (&ss) + ' 0 '; } if (z) &nBsp; // z Plastic Operations return; s[j++] = '. '; f = f - (int) f; while (i<23) { f = f*2; s[j+i] = (int) f + ' 0 '; f = f - (int) f; i++; }}void ieee754 (char* s) { int j=1,i = 0,q,e,jm=0; while (s[++i]!= '. '); int m=1; if (i>1) { while (s[m]!= ' 1 ' && m<strleN (s)) m++; //printf ("\n i = %d m= %d i - m = %d ", i,m,i - m-1); //Remove the sign bit jm = i-m-1; } else { while (s[m]!= ' 1 ' && m<strlen (s)) m++; //printf ("\n i = %d m= %d i - m = %d ", i,m,i - m); //Remove the sign bit jm = i-m; } int js = jm; jm = jm + 127; char cjm[16] = {0}; char ws[65]={0}; xd2b (jm,cjm,true); //reshape convert printf ("\ n sign bit: %c", s[0]); printf ("\ n order: %s\n ", &cjm[1]); for (Q= i -js,e=0; q<strlen (s); q++) { if (s[q] != '. ') ws[e++] = s[q]; } printf ("Mantissa: %s", WS); }void main () { printf ("Code by yanglong 0.o\n "); printf (" IEEE754&NBSP;&NBSP;->: "); char s[80]={0}; double df=0.0; &NBSP;&NBSP;SCANF ("%lf", &DF); xd2b (Df,s,false); printf ("%. 16lf binary decimal: ", DF); bool flag =true; int j = 0; for (Int i=0;i<strlen (s); i++) { putchar (S[i]); if (i == 0) { putchar (' '); //sign Bit } else if (i%4 ==0 ) //control putchar (' '); } ieee754 (s); printf ("\ n"); getchar (); getchar ();}
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/56/C4/wKiom1SNL5nB4Kd_AAEKEkfDx7Y190.jpg "title=" Xxxxx.jpg "alt=" Wkiom1snl5nb4kd_aaekekfdx7y190.jpg "/>
Convert decimal number to IEEE754 format