js之转码(base64、ascii、十六进制、url)

js之转码(base64、ascii、十六进制、url)

 base64

npm install js-base64

import { Base64 } from 'js-base64';

编码:

base64.encode("hello");

解码:

base64.decode("hello");

ascii

let url = "支持ASCII编码与字符的相互转换。ABCD,./!"
  
        function toascii (str) {
            let strs = ""
            for(let i in str){
                let ii = str.charCodeAt(i)
                strs +=  '\\u' + ii
            }
            return strs
        }
        
        let s = toascii(url)
        console.log(s)

        function asciito(str){
            let strs = ""
            let array=str.split("\\u");
            console.log(array)
            for(let item of array){
                if(item!==""){
                    console.log(item)
                    let ii = String.fromCharCode(item)
                    strs +=  ii
                }
            }
            return strs
        }
        let a = asciito(s)
        console.log(a)

十六进制

// 字符串转十六进制
        function strToUtf8Bytes(str) {
            const utf8 = [];
            for (let ii = 0; ii < str.length; ii++) {
                let charCode = str.charCodeAt(ii);
                if (charCode < 0x80) utf8.push(charCode);
                else if (charCode < 0x800) {
                utf8.push(0xc0 | (charCode >> 6), 0x80 | (charCode & 0x3f));
                } else if (charCode < 0xd800 || charCode >= 0xe000) {
                utf8.push(0xe0 | (charCode >> 12), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f));
                } else {
                ii++;
                charCode = 0x10000 + (((charCode & 0x3ff) << 10) | (str.charCodeAt(ii) & 0x3ff));
                utf8.push(
                    0xf0 | (charCode >> 18),
                    0x80 | ((charCode >> 12) & 0x3f),
                    0x80 | ((charCode >> 6) & 0x3f),
                    0x80 | (charCode & 0x3f),
                );
                }
            }
            //兼容汉字,ASCII码表最大的值为127,大于127的值为特殊字符
            for(let jj=0;jj<utf8.length;jj++){
                var code = utf8[jj];
                if(code>127){
                    utf8[jj] = code - 256;
                }
            }
            var hexCharCode = [];
            var chars = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"];
            for(var i = 0; i < utf8.length; i++) {
                var bit = (utf8[i] & 0x0f0) >> 4;
                hexCharCode.push(chars[bit]);
                var bit = utf8[i] & 0x0f;
                hexCharCode.push(chars[bit]);
            }
        return hexCharCode.join("");
        }
        let str = strToUtf8Bytes("JS 字符串转换16进制");
        console.log(str)

        // 十六进制转字符串
        function hexToStr(hex,encoding) {
            var trimedStr = hex.trim();
            var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
            var len = rawStr.length;
            if (len % 2 !== 0) {
                alert("Illegal Format ASCII Code!");
                return "";
            }
            var curCharCode;
            var resultStr = [];
            for (var i = 0; i < len; i = i + 2) {
                curCharCode = parseInt(rawStr.substr(i, 2), 16);
                resultStr.push(curCharCode);
            }
            // encoding为空时默认为utf-8
            var bytesView = new Uint8Array(resultStr);
            var str = new TextDecoder(encoding).decode(bytesView);
            return str;
        }
        let str1 = hexToStr('4A5320E5AD97E7ACA6E4B8B2E8BDACE68DA23136E8BF9BE588B6','utf-8')
        console.log(str1)

url

let url = "http://localhost:8080/index.html?time=2022-01-09&title=63元黑糖颗粒固饮"
  
        //部分编码解码
        // encodeURI()编码:
        let encodeURI_url = encodeURI(url) 
        console.log(encodeURI_url)
        // decodeURI()解码:
        let str = decodeURI(encodeURI_url)
        console.log(str)

        //全部部分编码解码
        // encodeURIComponent()编码:
        let encodeURI_url2 = encodeURIComponent(url) 
        console.log(encodeURI_url2)
        // decodeURIComponent()解码:
        let str2 = decodeURIComponent(encodeURI_url2)
        console.log(str2)

 

 

 

 

钻研不易,转载请注明出处。。。。

 

posted @ 2023-07-28 17:22  莫小龙  阅读(1620)  评论(0编辑  收藏  举报