473,399 Members | 4,192 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,399 software developers and data experts.

String & ASCII code

Hello,

I've just started programming with C++, and I'm looking for a way (function)
to convert numbers (int,float or double) into ASCII.

Thanks
Nov 17 '05 #1
2 1091
Nigel Sypouo wrote:
Hello,

I've just started programming with C++, and I'm looking for a way
(function) to convert numbers (int,float or double) into ASCII.


The old 'C' way would be to use sprintf. The following simple example
illustrates the use, but it full of potential problems (sprintf is one of
the biggest contributors to buffer overrun bugs).

<code>
#include <stdio.h>

void f(int i)
{
char sz[20];
sprintf(sz,"%d",i);
}
</code>

The modern 'C++' way would be to use ostringstream. The following sample
illustrates usage.

<code>
#include <sstream>
#include <string>

using namespace std;

string to_string(int i)
{
ostringstream os;
os << i;
return os.str();
}
</code>

I'd suggest looking up ostringstream in your favorite C++ book and learn
more about it.

-cd
Nov 17 '05 #2
Greetings,

I'm a self taught C++ user, and I can highly recommend the QUE series of
reference books at your local bookstore. Get both C and C++ books. I remember
being frustrated trying to look in C++ help documentation for lots of
questions like yours, that were really C issues. Since C++ is built on C,
too often the help documentation presumes the reader has a working knowledge
of C, and focuses only on C++ features like classes and objects.

I've also gotten some SAMS books, but I like the QUE much better. Stay away
from the "XXX for Dummies" books, the discussion is too superficial in my
opinion.

Enjoy,

Mike

"Nigel Sypouo" wrote:
Hello,

I've just started programming with C++, and I'm looking for a way (function)
to convert numbers (int,float or double) into ASCII.

Thanks

Nov 17 '05 #3

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

Similar topics

7
by: BlueDragon | last post by:
I don't know enough math to demonstrate that any numerical operation with a null should yield a null; although I would guess that it's true. I just don't buy it, however, when dealing with strings...
11
by: Kai Bohli | last post by:
Hi all ! I need to translate a string to Ascii and return a string again. The code below dosen't work for Ascii (Superset) codes above 127. Any help are greatly appreciated. protected...
18
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
18
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a...
7
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
8
by: Edson Manoel | last post by:
I have some C++ unmanaged code that takes std::string& arguments (as reference), and fills them (possibly growing the string). I want to call this code through PInvoke (DllImport), possibly using...
13
by: Eps | last post by:
Hi there, I believe all strings in .net are unicode by default, I am looking for a way to remove all non ascii characters from a string (or optionally replace them). There is an article on...
19
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that...
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?
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:
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...
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.