views:

133

answers:

6

What's the easiest way to convert an uint64 value into a standart C++ string? I checked out the assign methods from the string and could find no one that accepts an uint64 (8 bytes) as argument.

How can I do this?

Thanks Nelson R. Pérez

+4  A: 

I think you want to output it to a stringstream. Start here:

http://www.cppreference.com/wiki/io/sstream/start

Chris
I don't like this kind of answers. Can you provide some more description and/or some sample code for this specific question?
Lorenzo
+3  A: 

C++: Use a stringstream

C: sprintf (buffer,"%I64ld",myint64);

Patrick
Where from the C variant comes? Standard inttypes' way known to me is `sprintf(buffer,PRId64,myint64);`
Dummy00001
+7  A: 

more descriptive than streams I think is lexical_cast

uint64 somevalue;
string result = boost::lexical_cast<string>(somevalue);
Greg Domjan
He did mention standard C++ so I assume he meant STL. Still, Boost is a good idea, in general.
Randolpho
Standard C++ string..could refer to Char* string, a <string> string, or if he's a windows programmer, a CString or System::String.:-S Too many strings available! :-D
Caladain
You're right Randolpho, I meant STL. I'm not using Boost in this case. Thanks for the comment anyway Greg
Bilthon
IMHO it's pretty important to point out boost where applicable since so much time is wasted reinventing boost by people who may not know about it. Even if they do, it's worth pointing out that they're wasting time...AGAIN. Boost is not only standard C++, the authors make sure it even works on compilers that don't comply with the standard (pretty much all of them). In most cases where boost does what you need you will simply never do better.
Noah Roberts
Alrighty, my bad, I took that as 'standard C++ string' rather than an all encompassing 'standard C++' though which standard would be a further question. Anyway, lexical_cast 'is just' the prettier form of using the stringstream, rolling it up into a named function helps readability.
Greg Domjan
+3  A: 
#include <sstream>

std::ostringstream sin;
uint64 i;
sin << i;
std:string intAsString(oss.str());
Randolpho
+8  A: 

The standard way:

std::string uint64_to_string( uint64 value ) {
    std::ostringstream os;
    os << value;
    return os.str();
}

If you need an optimized method, then you may use this one:

void uint64_to_string( uint64 value, std::string& result ) {
    result.clear();
    result.reserve( 20 ); // max. 20 digits possible
    uint64 q = value;
    do {
        result += "0123456789"[ q % 10 ];
        q /= 10;
    } while ( q );
    std::reverse( result.begin(), result.end() );
}
frunsi
Great frunsi, it worked.. thanks a lot!
Bilthon
A: 
std::string converted(reinterpret_cast<char*>(&my_int64), reinterpret_cast<char*>((&my_int64)+1));
Noah Roberts
While this is technically converting a uint64 to a string, it is certainly not what the OP wanted. If so, he would have said "how do I convert the 8 constituent bytes of my uint64 into their ASCII equivalent characters in a string?". Or something similar to that.
A. Levy
This shouldn't be negged. It's the only answer that actually answers the question. OP didn't ask how to create a string representation of the value in a uint64.
Noah Roberts
@Levy - actually, the OP specifically mentions byte size and complains that there's no assign() for uint64. Every other assign in std::string does a direct byte assignment; no interpretation is done.
Noah Roberts
Sorry for not being explicit about that,but A.Levy is right. What I wanted was to take the value out of the number and make a string which represented that same number. I just didn't want to cast it to regular int and use sprintf because then I would be loosing half my bytes values and in some cases might get the wrong value.
Bilthon