• 字符串base64与二进制互转(兼容ie8)


    
    
    function stringToByte(str) {
        var bytes = new Array();
        var len, c;
        len = str.length;
        for (var i = 0; i < len; i++) {
            c = str.charCodeAt(i);
            if (c >= 0x010000 && c <= 0x10FFFF) {
                bytes.push(((c >> 18) & 0x07) | 0xF0);
                bytes.push(((c >> 12) & 0x3F) | 0x80);
                bytes.push(((c >> 6) & 0x3F) | 0x80);
                bytes.push((c & 0x3F) | 0x80);
            } else if (c >= 0x000800 && c <= 0x00FFFF) {
                bytes.push(((c >> 12) & 0x0F) | 0xE0);
                bytes.push(((c >> 6) & 0x3F) | 0x80);
                bytes.push((c & 0x3F) | 0x80);
            } else if (c >= 0x000080 && c <= 0x0007FF) {
                bytes.push(((c >> 6) & 0x1F) | 0xC0);
                bytes.push((c & 0x3F) | 0x80);
            } else {
                bytes.push(c & 0xFF);
            }
        }
        return bytes;

    }

    function
    utf8ByteToUnicodeStr(utf8Bytes){ var unicodeStr =""; for (var pos = 0; pos < utf8Bytes.length;){ var flag= utf8Bytes[pos]; var unicode = 0 ; if ((flag >>>7) === 0 ) { unicodeStr+= String.fromCharCode(utf8Bytes[pos]); pos += 1; } else if ((flag &0xFC) === 0xFC ){ unicode = (utf8Bytes[pos] & 0x3) << 30; unicode |= (utf8Bytes[pos+1] & 0x3F) << 24; unicode |= (utf8Bytes[pos+2] & 0x3F) << 18; unicode |= (utf8Bytes[pos+3] & 0x3F) << 12; unicode |= (utf8Bytes[pos+4] & 0x3F) << 6; unicode |= (utf8Bytes[pos+5] & 0x3F); unicodeStr+= String.fromCharCode(unicode) ; pos += 6; }else if ((flag &0xF8) === 0xF8 ){ unicode = (utf8Bytes[pos] & 0x7) << 24; unicode |= (utf8Bytes[pos+1] & 0x3F) << 18; unicode |= (utf8Bytes[pos+2] & 0x3F) << 12; unicode |= (utf8Bytes[pos+3] & 0x3F) << 6; unicode |= (utf8Bytes[pos+4] & 0x3F); unicodeStr+= String.fromCharCode(unicode) ; pos += 5; } else if ((flag &0xF0) === 0xF0 ){ unicode = (utf8Bytes[pos] & 0xF) << 18; unicode |= (utf8Bytes[pos+1] & 0x3F) << 12; unicode |= (utf8Bytes[pos+2] & 0x3F) << 6; unicode |= (utf8Bytes[pos+3] & 0x3F); unicodeStr+= String.fromCharCode(unicode) ; pos += 4; } else if ((flag &0xE0) === 0xE0 ){ unicode = (utf8Bytes[pos] & 0x1F) << 12;; unicode |= (utf8Bytes[pos+1] & 0x3F) << 6; unicode |= (utf8Bytes[pos+2] & 0x3F); unicodeStr+= String.fromCharCode(unicode) ; pos += 3; } else if ((flag &0xC0) === 0xC0 ){ //110 unicode = (utf8Bytes[pos] & 0x3F) << 6; unicode |= (utf8Bytes[pos+1] & 0x3F); unicodeStr+= String.fromCharCode(unicode) ; pos += 2; } else{ unicodeStr+= String.fromCharCode(utf8Bytes[pos]); pos += 1; } } return unicodeStr; } console.log("base64转字节流",stringToByte('/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAALCAC5ALkBAREA/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/9oACAEBAAA/AP5Hv+CX/wDwS/8Aj7/wVm+Pvi/9nP8AZz8X/B/wX428F/B/X/jXqmqfGvX/ABp4c8K3HhXw540+H/gW+0/T77wL8P8A4j6vL4gl1f4j6HcWtrcaHa6c+nWuqzTarBcwWdnffu9/xBU/8FTf+i+fsAf+HT/aK/8AoVaP')) // base64转字节流 [47, 57, 106, 47, 52, 65, 65, 81, 83, 107, 90, 74, 82, 103, 65, 66, 65, 81, 65, 65, 65, 81, 65, 66, 65, 65, 68, 47, 50, 119, 66, 68, 65, 65, 69, 66, 65, 81, 69, 66, 65, 81, 69, 66, 65, 81, 69, 66, 65, 81, 69, 66, 65, 81, 69, 66, 65, 81, 69, 66, 65, 81, 69, 66, 65, 81, 69, 66, 65, 81, 69, 66, 65, 81, 69, 66, 65, 81, 69, 66, 65, 81, 69, 66, 65, 81, 69, 66, 65, 81, 69, 66, 65, 81, 69, 66, 65, 81, 69, 66, …] const bytesArr = stringToByte('/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAALCAC5ALkBAREA/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/9oACAEBAAA/AP5Hv+CX/wDwS/8Aj7/wVm+Pvi/9nP8AZz8X/B/wX428F/B/X/jXqmqfGvX/ABp4c8K3HhXw540+H/gW+0/T77wL8P8A4j6vL4gl1f4j6HcWtrcaHa6c+nWuqzTarBcwWdnffu9/xBU/8FTf+i+fsAf+HT/aK/8AoVaP') console.log('字节流转字符串',utf8ByteToUnicodeStr(bytesArr)) // 字节流转字符串 /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAALCAC5ALkBAREA/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/9oACAEBAAA/AP5Hv+CX/wDwS/8Aj7/wVm+Pvi/9nP8AZz8X/B/wX428F/B/X/jXqmqfGvX/ABp4c8K3HhXw540+H/gW+0/T77wL8P8A4j6vL4gl1f4j6HcWtrcaHa6c+nWuqzTarBcwWdnffu9/xBU/8FTf+i+fsAf+HT/aK/8AoVaP
  • 相关阅读:
    linux的usr目录的全称是什么
    python多线程与多进程及其区别
    redis禁用危险命令
    测试文档
    mysql5.7.23windows安装
    Nginx如何处理手机端和PC端跳转不同页面
    nginx if多条件判断
    centos7单用户模式修改密码
    Django其四
    Django简单搭建编辑页面
  • 原文地址:https://www.cnblogs.com/dcyd/p/13494618.html
Copyright © 2020-2023  润新知