Exemplo n.º 1
0
void host_id_from_string(const std::string& hash,
                         node_id::host_id_type& node_id) {
    if (hash.size() != (node_id.size() * 2)) {
        throw std::invalid_argument("string argument is not a node id hash");
    }
    auto j = hash.c_str();
    for (size_t i = 0; i < node_id.size(); ++i) {
        // read two characters, each representing 4 bytes
        auto& val = node_id[i];
        val  = hex_char_value(*j++) << 4;
        val |= hex_char_value(*j++);
    }
}
Exemplo n.º 2
0
static int hex_to_decimal(const char* szHex, int len)   
{   
    int result = 0;   
    for(int i = 0; i < len; i++)   
    {   
        result += (int)pow((float)16, (int)len-i-1) * hex_char_value(szHex[i]);   
    }   
    return result;   
}  
Exemplo n.º 3
0
bool equal(const std::string& hash,
           const node_id::host_id_type& node_id) {
    if (hash.size() != (node_id.size() * 2)) {
        return false;
    }
    auto j = hash.c_str();
    try {
        for (size_t i = 0; i < node_id.size(); ++i) {
            // read two characters, each representing 4 bytes
            std::uint8_t val;
            val  = hex_char_value(*j++) << 4;
            val |= hex_char_value(*j++);
            if (val != node_id[i]) {
                return false;
            }
        }
    }
    catch (std::invalid_argument&) {
        return false;
    }
    return true;
}