PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Question on converting unsigned long to std::string
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Question on converting unsigned long to std::string

Réponse
 
LinkBack Outils de la discussion
Vieux 10/10/2008, 06h12   #1
Ramesh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 10/10/2008, 06h23   #2
Ruben
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question on converting unsigned long to std::string

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(const string &descr)
669 {
670 string errormsg("ERROR: enter a number followed by either \"kilograms\" or \"pounds\"");
671 istringstream stream1;
672 stream1.str(descr);
673 if (!(stream1>>wgt_))
674 throw errormsg;
675 if(!(stream1>>unit_))
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

  Réponse avec citation
Vieux 10/10/2008, 09h15   #3
d.j.a.de.kok@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question on converting unsigned long to std::string

On Oct 10, 6:23 am, Ruben <ru...@www2.mrbrklyn.com> wrote:
> 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
  Réponse avec citation
Vieux 10/10/2008, 09h44   #4
Kai-Uwe Bux
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question on converting unsigned long to std::string

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
  Réponse avec citation
Vieux 10/10/2008, 09h58   #5
Juha Nieminen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question on converting unsigned long to std::string

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:stringstream os;
os << yourUnsignedValue;
std::string theValueAsString = os.str();
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 16h51.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12019 seconds with 13 queries