2

I am using a uint128_t as an id type for a contract I am working on. For a certain check, I need to convert this variable to a string so I can check for it in another table (whose type cannot be changed). How can I convert the uint128_t variable to a string without having to#include "sstream", which is warned against by the compiler?

3

I don't know if this is the best solution but I used a little parser, which is closely connected to the eosio account name conversion from GitHub:

static const char* charmap = "0123456789";


std::string uint128ToString(const uint128_t& value)
{
    std::string result;
    result.reserve( 40 ); // max. 40 digits possible ( uint64_t has 20) 
    uint128_t helper = value;

    do {
        result += charmap[ helper % 10 ];
        helper /= 10;
    } while ( helper );
    std::reverse( result.begin(), result.end() );
    return result;
}

It will divide by 10 and use the charmap to identify the number representation to use.

  • This was the approach I ended up taking. It seems to work well. – John Haager Nov 13 '18 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.