electronics forum

Rules | Recent posts | topic RSS | Search | Register  | Log in

float to ascii conversion


Post new topic  Reply to topic    EDAboard.com Forum Index -> PC Programming and Interfacing -> float to ascii conversion
Author Message
momar128



Joined: 09 Sep 2003
Posts: 5
Location: Pakistan


Post24 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
Google Adsense




Post24 Sep 2003 11:12   

Ads




Back to top
artem



Joined: 22 May 2003
Posts: 1652
Helped: 91
Location: Turan


Post24 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


Post24 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



Sorry, but you need login in to view this attachment

Back to top
nebisman



Joined: 13 Apr 2002
Posts: 291
Helped: 1


Post24 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


Post24 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
Arabic versionBulgarian versionCatalan versionCzech versionDanish versionGerman versionGreek versionEnglish versionSpanish versionFinnish versionFrench versionHindi versionCroatian versionIndonesian versionItalian versionHebrew versionJapanese versionKorean versionLithuanian versionLatvian versionDutch versionNorwegian versionPolish versionPortuguese versionRomanian versionRussian versionSlovak versionSlovenian versionSerbian versionSwedish versionTagalog versionUkrainian versionVietnamese versionChinese version
Post new topic  Reply to topic    EDAboard.com Forum Index -> PC Programming and Interfacing -> float to ascii conversion
Page 1 of 1 All times are GMT + 1 Hour
Similar topics:
float to ascii (1)
float to whole number conversion (7)
float to fixed point conversion function (7)
Verilog - Float (16-bit) to real conversion (2)
Character to ASCII conversion (1)
DEC to ascii conversion (2)
Hex to ASCII conversion algorithm (4)
SMS PDU to ASCII conversion program (2)
16 bit to ascii conversion code in assembly language (1)
conversion from ascii text file to hexa (1)


Abuse || Administrator || Moderators || Support us || sitemap
topic RSS