i need to display a floating point number ranging from 0.1 to 0.001 over uart here is what i managed to search from forums and what not
unsigned long *chptr; float f=0.1; void main() { PINSEL0=0x5; uart0_open(); chptr= (unsigned long*)&f; uart0_printf("\n%x \n",f); sendchar(*chptr++); sendchar(*chptr++); sendchar(*chptr++); sendchar(*chptr++);
i am getting weird results any help masters?
another patch i worked with was
void main() { PINSEL0=0x5; uart0_open(); uart0_printf("Enter Data"); while(1) { for(i=0;i<6;i++) { ch[i]=getkey(); } data=chars2float(ch); uart0_printf("%x\n",data); float2chars(data,ch2); uart0_printf("\n%c%c%c%c%c",ch2[0],ch2[1],ch2[2],ch2[3],ch2[4]); } } float chars2float(char* parts) { union { unsigned long bits; float number; } floatbits; floatbits.bits = parts[0] + ((long)parts[1]<<7) + ((long)parts[2]<<14) + ((long)parts[3]<<21) + ((long)parts[4]<<28); return floatbits.number; } void float2chars(float input, char* parts) { union { unsigned long bits; float number; } floatbits; floatbits.number = input; parts[0] = floatbits.bits & ~0x80; // clear top bit parts[1] = (floatbits.bits >> 7) & ~0x80; parts[2] = (floatbits.bits >> 14) & ~0x80; parts[3] = (floatbits.bits >> 21) & ~0x80; parts[4] = (floatbits.bits >> 28); }
i am getting results here but the problem is i get a [] for the last digit.
any rectifications?
Remember that you are not the only programmer in the world. And you are not breaking a new ground also. Hence whatever problems you are facing, people around the world have face all those problems years back and have also come up with solutions that can be readily used.
Hence always search for a possible available solution. It is already existing in some corner of the world and Google can search it!!
Have you ever heard of Standard Libraries? Read sprintf, sscanf, atoi, ftoa etc.