2025年字符转码方法(字符转码方法有几种)

字符转码方法(字符转码方法有几种)lt script gt function base64Encode span style color rgba 0 0 255 1 return span

大家好,我是讯享网,很高兴认识大家。



<script> function base64EncodeChars() { 
讯享网</span><span style="color: rgba(0, 0, 255, 1)">return</span> "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0+/"<span style="color: rgba(0, 0, 0, 1)">; 
} function base64DecodeChars() {
</span><span style="color: rgba(0, 0, 255, 1)">var</span> dataDeco = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Array( </span>-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1<span style="color: rgba(0, 0, 0, 1)">, </span>-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1<span style="color: rgba(0, 0, 0, 1)">, </span>-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63<span style="color: rgba(0, 0, 0, 1)">, </span>52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1<span style="color: rgba(0, 0, 0, 1)">, </span>-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14<span style="color: rgba(0, 0, 0, 1)">, </span>15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1<span style="color: rgba(0, 0, 0, 1)">, </span>-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40<span style="color: rgba(0, 0, 0, 1)">, </span>41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> dataDeco; 
} function base64encode(str) {
讯享网
讯享网</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)"> out, i, len; </span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)"> c1, c2, c3; len </span>=<span style="color: rgba(0, 0, 0, 1)"> str.length; i </span>= 0<span style="color: rgba(0, 0, 0, 1)">; out </span>= ""<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">while</span> (i &lt;<span style="color: rgba(0, 0, 0, 1)"> len) { c1 </span>= str.charCodeAt(i++) &amp; 0xff<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">if</span> (i ==<span style="color: rgba(0, 0, 0, 1)"> len) { out </span>+= base64EncodeChars().charAt(c1 &gt;&gt; 2<span style="color: rgba(0, 0, 0, 1)">); out </span>+= base64EncodeChars().charAt((c1 &amp; 0x3) &lt;&lt; 4<span style="color: rgba(0, 0, 0, 1)">); out </span>+= "=="<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; } c2 </span>= str.charCodeAt(i++<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (i ==<span style="color: rgba(0, 0, 0, 1)"> len) { out </span>+= base64EncodeChars().charAt(c1 &gt;&gt; 2<span style="color: rgba(0, 0, 0, 1)">); out </span>+= base64EncodeChars().charAt(((c1 &amp; 0x3) &lt;&lt; 4) | ((c2 &amp; 0xF0) &gt;&gt; 4<span style="color: rgba(0, 0, 0, 1)">)); out </span>+= base64EncodeChars().charAt((c2 &amp; 0xF) &lt;&lt; 2<span style="color: rgba(0, 0, 0, 1)">); out </span>+= "="<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; } c3 </span>= str.charCodeAt(i++<span style="color: rgba(0, 0, 0, 1)">); out </span>+= base64EncodeChars().charAt(c1 &gt;&gt; 2<span style="color: rgba(0, 0, 0, 1)">); out </span>+= base64EncodeChars().charAt(((c1 &amp; 0x3) &lt;&lt; 4) | ((c2 &amp; 0xF0) &gt;&gt; 4<span style="color: rgba(0, 0, 0, 1)">)); out </span>+= base64EncodeChars().charAt(((c2 &amp; 0xF) &lt;&lt; 2) | ((c3 &amp; 0xC0) &gt;&gt; 6<span style="color: rgba(0, 0, 0, 1)">)); out </span>+= base64EncodeChars().charAt(c3 &amp; 0x3F<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> out; 
} function base64decode(str) {
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)"> c1, c2, c3, c4; </span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)"> i, len, out; len </span>=<span style="color: rgba(0, 0, 0, 1)"> str.length; i </span>= 0<span style="color: rgba(0, 0, 0, 1)">; out </span>= ""<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">while</span> (i &lt;<span style="color: rgba(0, 0, 0, 1)"> len) { </span><span style="color: rgba(0, 0, 255, 1)">do</span><span style="color: rgba(0, 0, 0, 1)"> { c1 </span>= base64DecodeChars()[str.charCodeAt(i++) &amp; 0xff<span style="color: rgba(0, 0, 0, 1)">]; } </span><span style="color: rgba(0, 0, 255, 1)">while</span> (i &lt; len &amp;&amp; c1 == -1<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (c1 == -1<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">do</span><span style="color: rgba(0, 0, 0, 1)"> { c2 </span>= base64DecodeChars()[str.charCodeAt(i++) &amp; 0xff<span style="color: rgba(0, 0, 0, 1)">]; } </span><span style="color: rgba(0, 0, 255, 1)">while</span> (i &lt; len &amp;&amp; c2 == -1<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (c2 == -1<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; out </span>+= String.fromCharCode((c1 &lt;&lt; 2) | ((c2 &amp; 0x30) &gt;&gt; 4<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 0, 255, 1)">do</span><span style="color: rgba(0, 0, 0, 1)"> { c3 </span>= str.charCodeAt(i++) &amp; 0xff<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">if</span> (c3 == 61<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> out; c3 </span>=<span style="color: rgba(0, 0, 0, 1)"> base64DecodeChars()[c3]; } </span><span style="color: rgba(0, 0, 255, 1)">while</span> (i &lt; len &amp;&amp; c3 == -1<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (c3 == -1<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; out </span>+= String.fromCharCode(((c2 &amp; 0XF) &lt;&lt; 4) | ((c3 &amp; 0x3C) &gt;&gt; 2<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 0, 255, 1)">do</span><span style="color: rgba(0, 0, 0, 1)"> { c4 </span>= str.charCodeAt(i++) &amp; 0xff<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">if</span> (c4 == 61<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> out; c4 </span>=<span style="color: rgba(0, 0, 0, 1)"> base64DecodeChars()[c4]; } </span><span style="color: rgba(0, 0, 255, 1)">while</span> (i &lt; len &amp;&amp; c4 == -1<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (c4 == -1<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; out </span>+= String.fromCharCode(((c3 &amp; 0x03) &lt;&lt; 6) |<span style="color: rgba(0, 0, 0, 1)"> c4); } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> out; 
} function utf16to8(str) {
讯享网</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)"> out, i, len, c; out </span>= ""<span style="color: rgba(0, 0, 0, 1)">; len </span>=<span style="color: rgba(0, 0, 0, 1)"> str.length; </span><span style="color: rgba(0, 0, 255, 1)">for</span> (i = 0; i &lt; len; i++<span style="color: rgba(0, 0, 0, 1)">) { c </span>=<span style="color: rgba(0, 0, 0, 1)"> str.charCodeAt(i); </span><span style="color: rgba(0, 0, 255, 1)">if</span> ((c &gt;= 0x0001) &amp;&amp; (c &lt;= 0x007F<span style="color: rgba(0, 0, 0, 1)">)) { out </span>+=<span style="color: rgba(0, 0, 0, 1)"> str.charAt(i); } </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (c &gt; 0x07FF<span style="color: rgba(0, 0, 0, 1)">) { out </span>+= String.fromCharCode(0xE0 | ((c &gt;&gt; 12) &amp; 0x0F<span style="color: rgba(0, 0, 0, 1)">)); out </span>+= String.fromCharCode(0x80 | ((c &gt;&gt; 6) &amp; 0x3F<span style="color: rgba(0, 0, 0, 1)">)); out </span>+= String.fromCharCode(0x80 | ((c &gt;&gt; 0) &amp; 0x3F<span style="color: rgba(0, 0, 0, 1)">)); } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { out </span>+= String.fromCharCode(0xC0 | ((c &gt;&gt; 6) &amp; 0x1F<span style="color: rgba(0, 0, 0, 1)">)); out </span>+= String.fromCharCode(0x80 | ((c &gt;&gt; 0) &amp; 0x3F<span style="color: rgba(0, 0, 0, 1)">)); } } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> out; 
} function utf8to16(str) {
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)"> out, i, len, c; </span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)"> char2, char3; out </span>= ""<span style="color: rgba(0, 0, 0, 1)">; len </span>=<span style="color: rgba(0, 0, 0, 1)"> str.length; i </span>= 0<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">while</span> (i &lt;<span style="color: rgba(0, 0, 0, 1)"> len) { c </span>= str.charCodeAt(i++<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">switch</span> (c &gt;&gt; 4<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">case</span> 0<span style="color: rgba(0, 0, 0, 1)">: </span><span style="color: rgba(0, 0, 255, 1)">case</span> 1<span style="color: rgba(0, 0, 0, 1)">: </span><span style="color: rgba(0, 0, 255, 1)">case</span> 2<span style="color: rgba(0, 0, 0, 1)">: </span><span style="color: rgba(0, 0, 255, 1)">case</span> 3<span style="color: rgba(0, 0, 0, 1)">: </span><span style="color: rgba(0, 0, 255, 1)">case</span> 4<span style="color: rgba(0, 0, 0, 1)">: </span><span style="color: rgba(0, 0, 255, 1)">case</span> 5<span style="color: rgba(0, 0, 0, 1)">: </span><span style="color: rgba(0, 0, 255, 1)">case</span> 6<span style="color: rgba(0, 0, 0, 1)">: </span><span style="color: rgba(0, 0, 255, 1)">case</span> 7<span style="color: rgba(0, 0, 0, 1)">: </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 0xxxxxxx</span> out += str.charAt(i - 1<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">case</span> 12<span style="color: rgba(0, 0, 0, 1)">: </span><span style="color: rgba(0, 0, 255, 1)">case</span> 13<span style="color: rgba(0, 0, 0, 1)">: char2 </span>= str.charCodeAt(i++<span style="color: rgba(0, 0, 0, 1)">); out </span>+= String.fromCharCode(((c &amp; 0x1F) &lt;&lt; 6) | (char2 &amp; 0x3F<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">case</span> 14<span style="color: rgba(0, 0, 0, 1)">: char2 </span>= str.charCodeAt(i++<span style="color: rgba(0, 0, 0, 1)">); char3 </span>= str.charCodeAt(i++<span style="color: rgba(0, 0, 0, 1)">); out </span>+= String.fromCharCode(((c &amp; 0x0F) &lt;&lt; 12) |<span style="color: rgba(0, 0, 0, 1)"> ((char2 </span>&amp; 0x3F) &lt;&lt; 6) |<span style="color: rgba(0, 0, 0, 1)"> ((char3 </span>&amp; 0x3F) &lt;&lt; 0<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; } } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> out; 
} var Base64 = function () {
讯享网</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> { encode: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (a) { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> base64encode(utf16to8(a)); }, decode: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (a) { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> utf8to16(base64decode(a)); } } 
}();

  // 使用 console.log(Base64.encode(“小星星”), Base64.decode(“5bCP5pif5pif”)) // 调用方式 &lt;/script&gt;

讯享网

小讯
上一篇 2025-04-30 17:09
下一篇 2025-06-12 12:51

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/206974.html