const sizeof = (str, charset = 'utf8') => {
let total = 0,
charCode,
i,
len;
charset = charset ? charset.toLowerCase() : '';
if (charset === 'utf-16' || charset === 'utf16') {
for (i = 0, len = str.length; i < len; i++) {
charCode = str.charCodeAt(i);
if (charCode <= 0xffff) {
total += 2;
} else {
total += 4;
}
}
} else {
for (i = 0, len = str.length; i < len; i++) {
charCode = str.charCodeAt(i);
if (charCode <= 0x007f) {
total += 1;
} else if (charCode <= 0x07ff) {
total += 2;
} else if (charCode <= 0xffff) {
total += 3;
} else {
total += 4;
}
}
}
return total;
}
主要是EOS DAPP开发的memo不能超过256bytes,这个可以用来在浏览器端计算memo长度
参考:http://www.alloyteam.com/2013/12/js-calculate-the-number-of-bytes-occupied-by-a-string/
赞助一杯咖啡☕️
本文由 widdy 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Nov 29, 2018 at 04:38 pm