float to ascii conversion

Status
Not open for further replies.

momar128

Newbie level 3
Joined
Sep 9, 2003
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Pakistan
Activity points
20
float to ascii

I need to convert floating point number into ASCII Character in C language.
Can anyone illustrate me with simple example.
 

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

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)
 

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);
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…