By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,742 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

Double to string?

P: n/a
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
Share this Question
Share on Google+
15 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.