473,406 Members | 2,387 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,406 software developers and data experts.

Double to string?

Hi,

How can I convert a double such as 23.1 to a string? I don't want to use
any library functions because the code is being written in avr-gcc and I
don't like the way dtostrf() has been implemented. Thanks.

Rick

Nov 13 '05 #1
15 39597
Rick <rrquick@nospam-com> wrote in news:3f********@clarion.carno.net.au:
Hi,

How can I convert a double such as 23.1 to a string? I don't want to use
any library functions because the code is being written in avr-gcc and I
don't like the way dtostrf() has been implemented. Thanks.


Don't know what avr-gcc and dtostrf are. However, have you considered
sprintf?

Sinan.

--
A. Sinan Unur
as**@c-o-r-n-e-l-l.edu
Remove dashes for address
Spam bait: mailto:uc*@ftc.gov
Nov 13 '05 #2
Rick <rrquick@nospam-com> writes:
How can I convert a double such as 23.1 to a string? I don't want to
use any library functions because the code is being written in avr-gcc
and I don't like the way dtostrf() has been implemented. Thanks.


I would refer to P.J. Plauger, _The Standard C Library_.
Alternatively, read the GNU libc implementation of sprintf (but
you won't like it.)
--
"I hope, some day, to learn to read.
It seems to be even harder than writing."
--Richard Heathfield
Nov 13 '05 #3
I can't use sprintf because it's not supported by the version of avr-gcc
(it's a compiler). I'm only interested in some "manual" way of
efficiently carrying a double to string conversion. Thanks
Rick

Ben Pfaff wrote:
Rick <rrquick@nospam-com> writes:

How can I convert a double such as 23.1 to a string? I don't want to
use any library functions because the code is being written in avr-gcc
and I don't like the way dtostrf() has been implemented. Thanks.

I would refer to P.J. Plauger, _The Standard C Library_.
Alternatively, read the GNU libc implementation of sprintf (but
you won't like it.)


Nov 13 '05 #4
Rick <rrquick@nospam-com> writes:
I can't use sprintf because it's not supported by the version of
avr-gcc (it's a compiler). I'm only interested in some "manual" way of
efficiently carrying a double to string conversion. Thanks
That's why I suggested reading an implementation of sprintf(),
not using sprintf(). sprintf() supports double to string
conversion with %e, %f, and %g.
Ben Pfaff wrote:
Rick <rrquick@nospam-com> writes:
How can I convert a double such as 23.1 to a string? I don't want to
use any library functions because the code is being written in avr-gcc
and I don't like the way dtostrf() has been implemented. Thanks.

I would refer to P.J. Plauger, _The Standard C Library_.
Alternatively, read the GNU libc implementation of sprintf (but
you won't like it.)


--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x1f6},*p=
b,x,i=24;for(;p+=!*p;*p/=4)switch(x=*p&3)case 0:{return 0;for(p--;i--;i--)case
2:{i++;if(1)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Nov 13 '05 #5
You can use the following logic:
1. Extract each digit of the number
2. Find the ascii equivalent of each number using a look up table [0..9]
3. Create a string

RS

"Rick" <rrquick@nospam-com> wrote in message
news:3f********@clarion.carno.net.au...
Hi,

How can I convert a double such as 23.1 to a string? I don't want to use
any library functions because the code is being written in avr-gcc and I
don't like the way dtostrf() has been implemented. Thanks.

Rick

Nov 13 '05 #6
in comp.lang.c i read:
I can't use sprintf because it's not supported by the version of
avr-gcc (it's a compiler). I'm only interested in some "manual" way of
efficiently carrying a double to string conversion. Thanks


the point to your reading an existing implementation is so you can gain an
understanding of how it can be accomplished.

--
a signature
Nov 13 '05 #7
Hi,

How confusing, I'm another Rick but I have the same question :) However, I
was thinking about the same way to convert... but howto extract a digit from
an integer, float?

Greetings,
Rick
Nov 13 '05 #8
Rick <rrquick@nospam-com> wrote in message news:<3f********@clarion.carno.net.au>...
Hi,

How can I convert a double such as 23.1 to a string? I don't want to use
any library functions because the code is being written in avr-gcc and I
don't like the way dtostrf() has been implemented. Thanks.

Rick


So sprintf() is out of the question?
Nov 13 '05 #9
In <3f********@clarion.carno.net.au> Rick <rrquick@nospam-com> writes:
How can I convert a double such as 23.1 to a string?
Are you sure you can have the value 23.1 represented by a double in the
first place?
I don't want to use
any library functions because the code is being written in avr-gcc and I
don't like the way dtostrf() has been implemented. Thanks.


If you're dealing with reasonable values (i.e. you don't have to resort to
the exponential format), it's quite straightforward.

Deal with the sign first and make the value positive if it was negative.
If the number is greater or equal to 1, keep dividing it by 10 until it
no longer is and count the number of divisions. Now, perform the
following simple steps:

1. If the division count is zero, insert the decimal point in the string.
2. Decrement the division count.
3. Multiply the value by 10.
4. Convert the integer part of the result to a digit code and insert it
into the string.
5. The fractional part becomes the new value you're dealing with.
6. If the division count is equal to the negative of the number of desired
decimals, insert the null terminator and you're done.
7. Go to step 1.

It may not be the fastest or the most accurate algorithm, but it is the
easiest to implement with no library support.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #10
On Thu, 4 Dec 2003 12:24:39 +0100, in comp.lang.c , "Rick"
<as******@hotmail.com> wrote:
Hi,

How confusing, I'm another Rick but I have the same question :) However, I
was thinking about the same way to convert... but howto extract a digit from
an integer, float?


This is an algorithm question

first consider that in C, you're guaranteed that '1'-'0' == 1
then consider how to determine the number of tens and units in 43,
using integer division
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #11
Rick <rrquick@nospam-com> wrote in message news:<3f********@clarion.carno.net.au>...

How can I convert a double such as 23.1 to a string? I don't want to use
any library functions because the code is being written in avr-gcc and I
don't like the way dtostrf() has been implemented. Thanks.


Assuming that dtostrf() converts a double to a string, the obvious
thing would seem to be to start from that and re-implement it in a
way that you like. Why does a dislike of the implementation of one
function stop you using other library functions?
Nov 13 '05 #12
Rick wrote:
Ben Pfaff wrote:
Rick <rrquick@nospam-com> writes:
How can I convert a double such as 23.1 to a string? I don't
want to use any library functions because the code is being
written in avr-gcc and I don't like the way dtostrf() has
been implemented. Thanks.


I would refer to P.J. Plauger, _The Standard C Library_.
Alternatively, read the GNU libc implementation of sprintf
(but you won't like it.)


I can't use sprintf because it's not supported by the version
of avr-gcc (it's a compiler). I'm only interested in some
"manual" way of efficiently carrying a double to string
conversion. Thanks


Don't toppost. Fixed this time.

Work on the longest integer type available to you, which will be
unsigned. The least significant decimal digit of this is
available as:

(value % 10) + '0';

You can extract all digits with a simple function such as:

void extract(unsigned value)
{
if (value / 10) extract(value / 10);
putchar('0' + value % 10);
}

Going from double to that depends on the binary structure of a
double, and is not really portable. You have to figure out how to
extract the necessary elements. float.h will help.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 13 '05 #13
"A. Sinan Unur" <as**@c-o-r-n-e-l-l.edu> wrote in
news:Xn****************************@132.236.56.8 on Wed 03 Dec 2003
08:29:14p:
Rick <rrquick@nospam-com> wrote in
news:3f********@clarion.carno.net.au:
Hi,

How can I convert a double such as 23.1 to a string? I don't want to
use any library functions because the code is being written in avr-gcc
and I don't like the way dtostrf() has been implemented. Thanks.


Don't know what avr-gcc and dtostrf are. However, have you considered
sprintf?


For safety's sake, use snprintf if you can: sprintf will smash a buffer
that's too small, writing on memory you don't actually own. snprintf takes
the size of the buffer as an extra argument, and so will not try to access
unallocated memory.

snprintf isn't mentioned in K&R2, however, so it might not be part of
Standard C. gcc, however, implements it, and it is a widespread extension
in any case. If you don't have access to a prewritten snprintf, it's
trivial to write one yourself.

Nov 13 '05 #14
August Derleth <li*****************@onewest.net> writes:
snprintf isn't mentioned in K&R2, however, so it might not be part of
Standard C.
snprintf() is in C99, although its return value is different from
what (some?) pre-standard implementations used.
gcc, however, implements it, and it is a widespread extension
in any case.
Actually GCC doesn't come with a C library, so it may or may not
be available with GCC. The GNU C library does include
snprintf().
If you don't have access to a prewritten snprintf, it's trivial
to write one yourself.


Really? Do you have a version that's better than using a
temporary file?
--
"I don't have C&V for that handy, but I've got Dan Pop."
--E. Gibbons
Nov 13 '05 #15
In <Xn**********************************@63.223.5.9 5> August Derleth <li*****************@onewest.net> writes:
"A. Sinan Unur" <as**@c-o-r-n-e-l-l.edu> wrote in
news:Xn****************************@132.236.56. 8 on Wed 03 Dec 2003
08:29:14p:
Rick <rrquick@nospam-com> wrote in
news:3f********@clarion.carno.net.au:
How can I convert a double such as 23.1 to a string? I don't want to ^^^^^^^^^^^^^^^ use any library functions because the code is being written in avr-gcc ^^^^^^^^^^^^^^^^^^^^^^^^^ and I don't like the way dtostrf() has been implemented. Thanks.


Don't know what avr-gcc and dtostrf are. However, have you considered
sprintf?


For safety's sake, use snprintf if you can: sprintf will smash a buffer
that's too small, writing on memory you don't actually own. snprintf takes
the size of the buffer as an extra argument, and so will not try to access
unallocated memory.

snprintf isn't mentioned in K&R2, however, so it might not be part of
Standard C. gcc, however, implements it, and it is a widespread extension
in any case. If you don't have access to a prewritten snprintf, it's
trivial to write one yourself.


What part of "I don't want to use any library functions" was too
difficult for all of you to understand?

If the OP is writing code for a freestanding implementation, he may not
have a <stdio.h> (and the corresponding library support) in the first
place.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #16

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

Similar topics

3
by: Vincent Cantin | last post by:
I have a class defined by a template which needs to "say" its type to the user via string. As an example, here is the class that I want to fix : template<class T> class Container : public...
1
by: kaede | last post by:
Hi all, Given the following data format: { "(1,2), "(2.5, 3.5)", .... } I would like to define a vector<string> to stored this data: v = "(1,2)", v = "(2.5, 3.5"), .... and would like to...
7
by: hana1 | last post by:
Hello experts, I used to program in C/C++ and now switched to Java. I am having a difficulty that I need your help with. How can I limit a double variable to hold 2 decimal points only? Say I...
4
by: joe_rattz | last post by:
I need to convert a text string ("Dewey & Cheatham & Howe") to an XML encoded string ("Dewey &amp; Cheatham &amp; Howe"). I am not building an XML document, I am just trying to convert a single string. I...
23
by: Rogers | last post by:
I want to compare strings of numbers that have a circular boundary condition. This means that the string is arranged in a loop without an end-of-string. The comparaison of two strings now...
3
by: sernamar | last post by:
Hi, I have the following base class class UnitOfMeasure { public: //... std::string& GetName() {return _uomName;}; //... protected:
1
by: Andrew Marlow | last post by:
Hello, I am new to java and need to write a program that uses an XML file for its configuration. I have an equivalent C++ program that uses libxml2 from gnome for this task. I wrapped this up in...
2
by: curious2007 | last post by:
I get this long and hard to understand warning when I call a constructor: AssocArray<double, string> myAssocArray(names, myArr); the code for the constructor: template <class V, class AI,...
2
by: Nicolai.Schoenberg | last post by:
When i try it like you say, he ignores the dot. For example s = "12.45"; d = double.Parse(s); d will be 1245.0 :( pls help
6
by: huohaodian | last post by:
Hi, How can I define a long string variable in C# with multiple lines ? For example private string longName = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.