uint8_t from_hex(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
eosio_assert(false, "Invalid hex character");
return 0;
}
size_t from_hex(const string& hex_str, char* out_data, size_t out_data_len) {
auto i = hex_str.begin();
uint8_t* out_pos = (uint8_t*)out_data;
uint8_t* out_end = out_pos + out_data_len;
while (i != hex_str.end() && out_end != out_pos) {
*out_pos = from_hex((char)(*i)) << 4;
++i;
if (i != hex_str.end()) {
*out_pos |= from_hex((char)(*i));
++i;
}
++out_pos;
}
return out_pos - (uint8_t*)out_data;
}
capi_checksum256 hex_to_sha256(const string& hex_str) {
eosio_assert(hex_str.length() == 64, "invalid sha256");
capi_checksum256 checksum;
from_hex(hex_str, (char*)checksum.hash, sizeof(checksum.hash));
return checksum;
}
string to_hex(const char* d, uint32_t s) {
std::string r;
const char* to_hex = "0123456789abcdef";
uint8_t* c = (uint8_t*)d;
for (uint32_t i = 0; i < s; ++i)
(r += to_hex[(c[i] >> 4)]) += to_hex[(c[i] & 0x0f)];
return r;
}
string sha256_to_hex(const capi_checksum256& sha256) {
return to_hex((char*)sha256.hash, sizeof(sha256.hash));
}
EOS string与checksum256转换
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一个介绍strconv库很好的博客(转化为字符串)https://blog.csdn.net/li_101357/...
- string转byte /** * string 转 byte * @param str */ privat...
- 问题一 字串前面少了u。当遇见以下情况。返回字符串为'\u82f9\u679c'的unicode时候。 解决方法:...
- 前言 CSDN博客地址 GitHub https://github.com/MrQ-Android 最近在操作蓝...
- 我今天知道了一个消息,惊讶的连下巴都合不上了。然后迅速的和朋友们八卦了一下,觉得这个故事里有好多值得想谈恋爱的姑娘...