473,395 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

float to unsigned char array

Hi,
i want to convert a floating point number to a character array, because I
want to save the number in an NVSRAM which can be described as:
unsigned char RAM[size];


Is there a C typical approach to convert the float number?

I used "fcvt()" to convert float to a string. But it seems its not part of
the standart language.

Thanks, Chris
Nov 13 '05 #1
7 17678
"C. Sengstock" <c.*********@urz.uni-heidelberg.de> writes:
Hi,
i want to convert a floating point number to a character array,


See strtod() (C99 also defines strtof()).
Nov 13 '05 #2
Thomas Pfaff <th**********@tiscali.no> writes:
"C. Sengstock" <c.*********@urz.uni-heidelberg.de> writes:
Hi,
i want to convert a floating point number to a character array,


See strtod() (C99 also defines strtof()).


Sorry, but that doesn't quite do what you asked for. Sigh.

See sprintf or snprintf (C99).
Nov 13 '05 #3
C. Sengstock wrote:
Hi,
i want to convert a floating point number to a character array, because I
want to save the number in an NVSRAM which can be described as:

unsigned char RAM[size];

#include <stdio.h>
#include <float.h>
int main(void)
{
char s[BUFSIZ];
sprintf(s, "%.*g", DBL_DIG, 0.00000037);
printf("Here's one such string: %s\n", s);
sprintf(s, "%.*g", DBL_DIG, 0.00037);
printf(" here's another: %s\n", s);
sprintf(s, "%.*g", DBL_DIG, 0.37);
printf(" here's another: %s\n", s);
sprintf(s, "%.*g", DBL_DIG, 370.);
printf(" here's another: %s\n", s);
sprintf(s, "%.*g", DBL_DIG, 370000.);
printf(" here's another: %s\n", s);
sprintf(s, "%.*g", DBL_DIG, 370000000.);
printf(" here's another: %s\n", s);
sprintf(s, "%.*g", DBL_DIG, 370000000000.);
printf(" here's the last: %s\n", s);
return 0;
}
Here's one such string: 3.7e-07
here's another: 0.00037
here's another: 0.37
here's another: 370
here's another: 370000
here's another: 370000000
here's the last: 370000000000
--
Martin Ambuhl

Nov 13 '05 #4
Thomas Pfaff wrote:
"C. Sengstock" <c.*********@urz.uni-heidelberg.de> writes:

Hi,
i want to convert a floating point number to a character array,

See strtod() (C99 also defines strtof()).

Reread it, Tom. He wants to go the other way, for which sprintf() is the
choice.

--
Martin Ambuhl

Nov 13 '05 #5
In 'comp.lang.c', "C. Sengstock" <c.*********@urz.uni-heidelberg.de> wrote:
i want to convert a floating point number to a character array, because I
I assume you mean a 'string'.
want to save the number in an NVSRAM which can be described as:
unsigned char RAM[size];


Is there a C typical approach to convert the float number?

I used "fcvt()" to convert float to a string. But it seems its not part of
the standart language.


What's wrong with sprintf() (or snprintf() if you have C99). Time to reopen
your C-book...

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #6

"C. Sengstock" <c.*********@urz.uni-heidelberg.de> wrote in message
news:bk**********@news.urz.uni-heidelberg.de...
Hi,
i want to convert a floating point number to a character array, because I
want to save the number in an NVSRAM which can be described as:
unsigned char RAM[size];


Is there a C typical approach to convert the float number?


Use sprintf( )

Make sure that your char buffer is big enough to hold the result. (If your
compiler support C99, you can use snprintf( ) to avoid buffer overflow)
--
Jeff
-je6543 at yahoo.com
Nov 13 '05 #7
C. Sengstock <c.*********@urz.uni-heidelberg.de> wrote:
Hi,
i want to convert a floating point number to a character array, because I
want to save the number in an NVSRAM which can be described as:
unsigned char RAM[size];


Is there a C typical approach to convert the float number?

I used "fcvt()" to convert float to a string. But it seems its not part of
the standart language.


You can just use memcpy(), as long as you're not moving your NVSRAM
between systems.

- Kevin.

Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in...
5
by: Vijay Kumar R Zanvar | last post by:
Hi, Just like counting the number of bits set to 1 in an integral variable, can we count the same in a float? I have two solutions in my mind: 1. union { float f;
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
9
by: Gregory.A.Book | last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have a library that reads image data and returns the data as an array of unsigned chars. The image data is stored as 4-byte floats....
7
by: Stein Gulbrandsen | last post by:
What is the best way to get to the integer representation of a float? I would like to do int& toIntByCast (float& a) {return *reinterpret_cast<int*(&a);} but is this legal? Is this safer?...
9
by: hurcan solter | last post by:
Hi all, I am trying to convert a float value to a octet stream for transmission, I came up with solution like float deneme=3.14156789; float deneme2=0.0; vector<unsigned charvec; //this is my...
45
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
9
by: mathieu | last post by:
Hi, I know I am doing something stupid here, but it's friday night and I cannot see what is the issue here: Thanks, -Mathieu #include <iostream>
37
by: The87Boy | last post by:
Hey all I have a problem with float I should write a program, where you are getting some numbers from the command-line and try to find the maximum and minimum-values of these numbers. I need...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.