本帖最后由 hotpower 于 2023-9-22 14:18 编辑
菜农电子淘宝:https://hotcomlock.taobao.com/ 准备出书:http://hotcomlock.com/hotcomlock.html
- function HexToBase64(hexstr)
- {
- var i, str, hexstrString;
- hexstrString = "";
- for (i = 0; i < hexstr.length; i += 8)
- {
- hexstrString += HexStrToBase64(hexstr.substr(i, 8));
- }
- return hexstrString;//输出3N个字节
- }
- function GetBase64Value(ch)
- {
- var value = 0;
- if((ch >= "A") && (ch <= "Z")) {
- value = ch.charCodeAt(0) - 0x41;
- }
- else if((ch >= "a") && (ch <= "z")) {
- value = ch.charCodeAt(0) - 0x61 + 26;
- }
- else if((ch >= "0") && (ch <= "9")) {
- value = ch.charCodeAt(0) - 0x30 + 52;
- }
- else if(ch == "+") {
- value = 62;
- }
- else if(ch == "/") {
- value = 63;
- }
- return value;
- }
- function HexStrToBase64(basehexstr)
- {//输入4个字节
- var c, a, t, x1, x2, x3, x4, str, hexstr;
- str = HexToAscii(basehexstr);//4个字符
- x1 = GetBase64Value(str.substr(0, 1));
- x2 = GetBase64Value(str.substr(1, 1));
- x3 = GetBase64Value(str.substr(2, 1));
- x4 = GetBase64Value(str.substr(3, 1));
- hexstr = "";
- if(str.substr(3, 1) != "=") {
- c = (x1 << 2) | (x2 >>> 4);
- a = ((x2 & 0xf) << 4) | (x3 >>> 2);
- t = ((x3 & 0x03) << 6) | x4;
- hexstr = inttohex(c, 2);
- hexstr += inttohex(a, 2);
- hexstr += inttohex(t, 2);
- }
- else if(str.substr(2, 1) != "=") {
- c = (x1 << 2) | (x2 >>> 4);
- a = ((x2 & 0xf) << 4) | (x3 >>> 2);
- hexstr = inttohex(c, 2);
- hexstr += inttohex(a, 2);
- }
- else{
- c = (x1 << 2) | (x2 >>> 4);
- hexstr = inttohex(c, 2);
- }
- return hexstr;//输出3个字节
- }
|