473,692 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question on converting unsigned long to std::string

Hi,

I need to convert unsigned long data to a std::string, I googled a bit
to see if the string class supports any methods to achieve this - but
didnt find any, I think of using sprintf and converting to a char buf
and then putting it back in std::string -

is anyone aware of any other way?

Thanks
/R
Oct 10 '08 #1
4 6501
On Thu, 09 Oct 2008 21:12:51 -0700, Ramesh wrote:
Hi,

I need to convert unsigned long data to a std::string, I googled a bit
to see if the string class supports any methods to achieve this - but
didnt find any, I think of using sprintf and converting to a char buf
and then putting it back in std::string -

is anyone aware of any other way?

Thanks
/R
istringstream is one way without resorting to the C libraries

see this example

http://www.nylxs.com/docs/workshops/pharm.C.html

look around line 670
668 float Weight::wgt(con st string &descr)
669 {
670 string errormsg("ERROR : enter a number followed by either \"kilograms\ " or \"pounds\"") ;
671 istringstream stream1;
672 stream1.str(des cr);
673 if (!(stream1>>wgt _))
674 throw errormsg;
675 if(!(stream1>>u nit_))
676 throw errormsg;
677 errormsg = "ERROR: enter either \"kilograms\ " or \"pounds\"";
678 if( unit_ == "kg" || unit_ == "k" || unit_ == "kilo" || unit_ == "kilogram"| | unit_ == "kilograms" ){
679 unit_ = "kg";
680 }else if( unit_ == "lb" || unit_ == "pound" || unit_ == "pd"|| unit_ == "pounds" ){
681 wgt_ = wgt_ * 0.45454545;
682 }else{
683 throw errormsg;
684 }
685 return wgt_;
686 }
687

--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Oct 10 '08 #2
On Oct 10, 6:23 am, Ruben <ru...@www2.mrb rklyn.comwrote:
istringstream is one way without resorting to the C libraries
Since he wants to convert a number to a string, I'd rather use
ostringstream. An example can be found at:

http://www.parashift.com/c++-faq-lit....html#faq-39.1

-- Daniel
Oct 10 '08 #3
Ramesh wrote:
Hi,

I need to convert unsigned long data to a std::string, I googled a bit
to see if the string class supports any methods to achieve this - but
didnt find any, I think of using sprintf and converting to a char buf
and then putting it back in std::string -

is anyone aware of any other way?
If you are used to sprintf but want something that deals with std::string
instead, you could try this:

bool strprintf ( std::string & buffer, char const * format, ... ) {
while ( true ) {
buffer.resize( buffer.capacity () );
int old_length = buffer.size();
std::va_list aq;
va_start( aq, format );
int length_needed =
vsnprintf( &buffer[0], old_length + 1, format, aq );
va_end( aq );
if ( length_needed < 0 ) {
return ( false );
}
buffer.resize( length_needed );
if ( length_needed <= old_length ) {
return ( true );
}
}
}
WARNING: the above is not standard compliant; it assumes that std::string is
contiguous (as far as I know, all implementations are, and it will be
required by the next iteration of the standard C++0X); also it requires
that is is legal to write a trailing 0 behind the string. Also, it assumes
that vsnprintf() conforms to C99 or C++0X. Thus, you have to test this
carefully.
Best

Kai-Uwe Bux
Oct 10 '08 #4
Ramesh wrote:
is anyone aware of any other way?
I really can't understand why two people have already answered your
question with overly long and complicated answers, which are also
basically just wrong. Come on, people, keep answers simple and
concentrate on the *basics*.

What you want is this:

std::ostringstr eam os;
os << yourUnsignedVal ue;
std::string theValueAsStrin g = os.str();
Oct 10 '08 #5

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

Similar topics

1
12273
by: red floyd | last post by:
I have a an app that I'm writing which uses char and std::string. I'm using a library which expects wchar_t arrays. Is there a standard way to convert between std::string and std::wstring, or do I need to use something like std::transform()? Thanks
13
2487
by: Gil | last post by:
I have a string : string A = "2"; I need to get it's integer value : // ?? int val = A ; // ??
7
117993
by: AlexFarokhyans | last post by:
Hello, I'm trying to convert String to Char array. I'm getting a string from user input text box and then I have char firstName. I need to convert the string that is in the text box to firstName. Thank you
5
15909
by: Torben Laursen | last post by:
Hi Can anyone show me how to convert between VARIANT and std::string and back, thanks! Torben ---
2
4805
by: zhege | last post by:
I am a beginner of C++; I have a question about the std:string and std:cout class; Two pieces of code: -------------------------------- #include <iostream> #include <string> using namespace std; int main()
1
1727
by: zhege | last post by:
I am a beginner of C++; I have a question about the std:string and std:cout class; Two pieces of code: -------------------------------- #include <iostream> #include <string> using namespace std; int main()
2
10906
by: pookiebearbottom | last post by:
Just looking for opinion on which of the 3 methods below people use in their code when they convert a 'const char *' to a 'const std::string &' came across #3 in someone's code and I had to think for a sec. At first I read it as converting a 'const char *' to a 'std::string *' void f(const std::string &s) { std::cout << s.size() << "\n";
2
3089
by: =?Utf-8?B?QWJoaW1hbnl1IFNpcm9oaQ==?= | last post by:
Hi, I am using Visual C++ in Visual Studio 2005 to create a Managed Wrapper around some C++ LIBS. I've created some classes that contains a pointer to the LIB classes and everthing seems to compile well. The problem is getting a std::string from System::String and still preserving the nulls. System::String is a base64 encoded string of a series of bytes. When I convert it back to a string from base64 representation, the NULLs inside the...
12
1766
by: manishsharma1 | last post by:
Hi, I have one long String like this: 015EnvironmentData1........ I want to read first three character from it and convert them to integer. This will be the length of the Name of the field and then there is value. So if we take above example
0
8610
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8544
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8810
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7633
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, and deployment—without 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
6462
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
5821
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
4325
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
4559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2242
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.