473,882 Members | 1,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
15 39641
On Thu, 4 Dec 2003 12:24:39 +0100, in comp.lang.c , "Rick"
<as******@hotma il.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.c om/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@nosp am-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@nosp am-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(unsigne d 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********@yah oo.com) (cb********@wor ldnet.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.5 6.8 on Wed 03 Dec 2003
08:29:14p:
Rick <rrquick@nosp am-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.n et> 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.95> August Derleth <li************ *****@onewest.n et> 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@nosp am-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
2581
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 Serializable { public: T value;
1
2253
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 extract the integer/double string value to real int/double and use it in computation. What is the best way to do it?
7
43178
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 have an array of 50 doubles that each ahs a number such as 23.9918444. I want to round round this number to 23.99 and any other calculations done on it should have the same precision. I know that Decimal Format does the rounding but the thing that...
4
60005
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 have looked at the System.Xml namespace, but I can't quite find what I need. XmlTextWriter.WriteString() does the type of encoding I need, but it writes the string. I just want it to return the encoded string. Can someone tell me the class and...
23
2111
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 becomes a different operation than with regular strings because the circular string can be "rotated", like this: 1 2 3 4 5 2 3 4 5 1 3 4 5 1 2 4 5 1 2 3
3
1799
by: sernamar | last post by:
Hi, I have the following base class class UnitOfMeasure { public: //... std::string& GetName() {return _uomName;}; //... protected:
1
6241
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 a class (libxml2 is a C interface). I am wondering how people solve this problem in java. Googling reveals a tonne of xml configuration projects all pulling in different directions with different dependencies. The ones that stick out (IMHO)...
2
2753
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, class I, class S> AssocArray<V, AI, I, S>::AssocArray(const list<AI>& names, const Array<V, I, S>& source) {
2
2119
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
6872
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" "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
0
9931
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
11108
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10725
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10830
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9557
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7956
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7113
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5978
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.