Convert uint64 to string in C ++

What is the easiest way to convert a uint64 value to a standard C ++ string? I checked the assignment methods from the string and could not find anyone taking uint64 (8 bytes) as an argument.

How can i do this?

thanks

+6
c ++ string uint64
source share
7 answers
#include <sstream> std::ostringstream oss; uint64 i; oss << i; std:string intAsString(oss.str()); 
+9
source share

Standard way:

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

If you need an optimized method, you can use it:

 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() ); } 
+12
source share

more descriptive than threads, I think lexical_cast

 uint64 somevalue; string result = boost::lexical_cast<string>(somevalue); 
+9
source share

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

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

+4
source share

C ++: use stringstream

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

+4
source share

C ++ 11 standardized the to_string function mentioned by Fruncy as std::to_string :

 #include <string> int main() { uint64_t value = 128; std::string asString = std::to_string(value); return 0; } 
+2
source share
 std::string converted(reinterpret_cast<char*>(&my_int64), reinterpret_cast<char*>((&my_int64)+1)); 
-2
source share

All Articles