| Author |
Message |
momar128
Joined: 09 Sep 2003 Posts: 5 Location: Pakistan
|
24 Sep 2003 11:12 float to ascii |
|
|
|
|
I need to convert floating point number into ASCII Character in C language.
Can anyone illustrate me with simple example.
|
|
| Back to top |
|
 |
Google AdSense

|
24 Sep 2003 11:12 Ads |
|
|
|
|
|
|
| Back to top |
|
 |
artem
Joined: 22 May 2003 Posts: 1652 Helped: 91 Location: Turan
|
24 Sep 2003 16:37 float to ascii conversion in c |
|
|
|
|
- get the format those are stored (your C compiler documentation must tell that )in your memory
- get bytes from memory according to format above
and convert them byte by byte to the ascii acccording
to byte meaning .
|
|
| Back to top |
|
 |
C-Man
Joined: 19 Jul 2001 Posts: 1235 Helped: 73
|
24 Sep 2003 17:28 float to ascii in c |
|
|
|
|
This is highly machine dependant, you should be able to what you need if your compiler has the sprintf library function which acts like printf but puts the result into a buffer (memory) from where you can extract the ascii representation of your float.
I also included a routine I found on the net, but as said before this is machine dependant!!!
If possible use sprintf.
hope this helps
|
|
| Back to top |
|
 |
nebisman
Joined: 13 Apr 2002 Posts: 291 Helped: 1
|
24 Sep 2003 20:40 ieee754 ascii conversion |
|
|
|
|
If you use the IEEE 754 (most usual) norm for float remember:
bit 31 sign
bit 23-30 exponent with excess to 127 (0 es 127)
bit 0 -22 mantissa
then you can eloborate a function which is composed union mixed with estructure for return these fields and then traduce then to ASCII. This union return the fields in BYTES for trasmision over a RS232 channel.
| Code: |
union {
double num_float;
long int num_longint;
int num_int;
struct dividir{
char byte3;
char byte2;
char byte1;
char byte0;
}bytes;
}number;
and access to fields for this example:
void
tx_float (double num_tx){
numero.num_float = num_tx;
putch(numero.bytes.byte3);
putch(numero.bytes.byte2);
putch(numero.bytes.byte1);
putch(numero.bytes.byte0);
}
|
then only set the fields for return the sign, exponent,mantissa, if I understand your problem, and with simple function convert it to ASCII code ex. -1.25 e 24. (sign ,mantissa ,exponen)
|
|
| Back to top |
|
 |
amir81
Joined: 01 Mar 2003 Posts: 44 Helped: 5
|
24 Sep 2003 20:45 float to ascii conversion |
|
|
|
|
you can use thses functions:
AnsiString __fastcall FloatToStr(Extended Value);
AnsiString __fastcall FloatToStrF(Extended Value, TFloatFormat Format, int Precision, int Digits);
int __fastcall FloatToText(char * Buffer, const void *Value, TFloatValue ValueType, TFloatFormat Format, int Precision, int Digits);
int __fastcall FloatToTextFmt(char * Buffer, const void *Value, TFloatValue ValueType, char * Format);
|
|
| Back to top |
|
 |