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?

up vote 2 down vote accepted

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 at 21:56

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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