Skip to content

Instantly share code, notes, and snippets.

@LRENZ
Last active August 25, 2021 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LRENZ/a5339da224293e1f30a0867d94ead8bd to your computer and use it in GitHub Desktop.
Save LRENZ/a5339da224293e1f30a0867d94ead8bd to your computer and use it in GitHub Desktop.
convert_28_openid_to_38_digital number.js
var str = 'od3P-t1o5NOTl4ketBHZ1kZ0DBio';
function convert(str) {
if (str.length == 28) {
var binary = []
var num = []
var binary_str
for (var i = 0; i < str.length; i++) {
var binStr = str.charCodeAt(i).toString(2).padStart(8, "0"); // 补全为8位数字 ex:01110110
binary.push(binStr);
}
binary_str = binary.join("")+"0000" //补全为 228 = 6x38
binary_str = binary_str.match(/.{6}/g) // 上面字符每 6个分成一组,总共38组
for (var j = 0; j < binary_str.length; j++) {
var dec = parseInt(binary_str[j], 2) //每一组转成10进制
//console.log(dec)
num.push(dec % 10) //取个位数
}
return num.join("") //补全最后一位
}
}
var t = convert(str)
t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment