suppose I declare:

struct [[eosio::table]] tst {
   asset component;
   uint64_t primary_key() const {
      return component.symbol.code().raw();

where I mean to keep a list of assets. when it's time to find a record in the table I want to do something like:

void token::find(char *symbol) {
   tst t(_self, _self.value);
   auto o = t.find(symbol);

but this results in an error:

t.cpp:127:20: error: cannot initialize a parameter of type 'uint64_t' (aka 'unsigned long long') with an lvalue of type 'char *' auto o = t.find(symbol);

changing the parameter definition to:

void token::find(std::string symbol) {

produces a similar message:

t.cpp:127:20: error: no viable conversion from 'std::string' (aka 'basic_string, allocator >') to 'uint64_t' (aka 'unsigned long long') auto o = t.find(symbol);

in other code I've seen these finds done given an asset. something like this works:

auto o = t.find(someAsset.symbol.code().raw());

which ultimately resolves to a uint64_t

so I need somehow to convert my string to an integer. can anyone provide a hint as to how I might do this?


When you are lost with EOSIO native types, you can look it up at eosio.cdt's eosiolib library (github)

A explicit constructor is defined as following inside symbol.hpp

constexpr explicit symbol_code( std::string_view str )
         if( str.size() > 7 ) {
            eosio::check( false, "string is too long to be a valid symbol_code" );
         for( auto itr = str.rbegin(); itr != str.rend(); ++itr ) {
            if( *itr < 'A' || *itr > 'Z') {
               eosio::check( false, "only uppercase letters allowed in symbol_code string" );
            value <<= 8;
            value |= *itr;

symbol.hpp is included in asset.hpp so it should be fine to include just theeosiolib/asset.hpp as usual.

  • thanks for the reply. I guess the answer is I can do something like symbol::symbol_code("USD"). I've marked your reply as the answer even though it doesn't actually answer the question
    – ekkis
    Jan 25 '19 at 23:05
  • actually, the above doesn't work. so I'll reopen this question
    – ekkis
    Jan 2 '20 at 5:43

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.