Converting an uint64 to string in C++
Asked Answered
T

7

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

Tuning answered 24/6, 2010 at 19:53 Comment(0)
S
11
#include <sstream>

std::ostringstream oss;
uint64 i;
oss << i;
std:string intAsString(oss.str());
Salena answered 24/6, 2010 at 19:57 Comment(0)
A
13

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() );
}
Adjudicate answered 24/6, 2010 at 20:2 Comment(0)
S
11
#include <sstream>

std::ostringstream oss;
uint64 i;
oss << i;
std:string intAsString(oss.str());
Salena answered 24/6, 2010 at 19:57 Comment(0)
W
9

more descriptive than streams I think is lexical_cast

uint64 somevalue;
string result = boost::lexical_cast<string>(somevalue);
Wade answered 24/6, 2010 at 19:57 Comment(5)
He did mention standard C++ so I assume he meant STL. Still, Boost is a good idea, in general.Salena
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! :-DAerolite
You're right Randolpho, I meant STL. I'm not using Boost in this case. Thanks for the comment anyway GregTuning
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.Rival
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.Wade
W
5

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

#include <string>

int main()
{
  uint64_t value = 128;
  std::string asString = std::to_string(value);
  return 0;
}
Waterage answered 7/11, 2017 at 16:22 Comment(0)
L
4

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

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

Lightner answered 24/6, 2010 at 19:54 Comment(1)
I don't like this kind of answers. Can you provide some more description and/or some sample code for this specific question?Agateware
E
4

C++: Use a stringstream

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

Endoenzyme answered 24/6, 2010 at 19:55 Comment(1)
Where from the C variant comes? Standard inttypes' way known to me is sprintf(buffer,PRId64,myint64);Buttons
R
-1
std::string converted(reinterpret_cast<char*>(&my_int64),
                      reinterpret_cast<char*>((&my_int64)+1));
Rival answered 24/6, 2010 at 20:23 Comment(4)
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.Kiushu
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.Rival
@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.Rival
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.Tuning

© 2022 - 2024 — McMap. All rights reserved.