<small id='wPBGj'></small><noframes id='wPBGj'>

        <bdo id='wPBGj'></bdo><ul id='wPBGj'></ul>
      <i id='wPBGj'><tr id='wPBGj'><dt id='wPBGj'><q id='wPBGj'><span id='wPBGj'><b id='wPBGj'><form id='wPBGj'><ins id='wPBGj'></ins><ul id='wPBGj'></ul><sub id='wPBGj'></sub></form><legend id='wPBGj'></legend><bdo id='wPBGj'><pre id='wPBGj'><center id='wPBGj'></center></pre></bdo></b><th id='wPBGj'></th></span></q></dt></tr></i><div id='wPBGj'><tfoot id='wPBGj'></tfoot><dl id='wPBGj'><fieldset id='wPBGj'></fieldset></dl></div>
    1. <legend id='wPBGj'><style id='wPBGj'><dir id='wPBGj'><q id='wPBGj'></q></dir></style></legend>

      1. <tfoot id='wPBGj'></tfoot>
      2. JavaScript 中的 UTF-16 到 UTF-8 转换

        UTF-16 to UTF-8 conversion in JavaScript(JavaScript 中的 UTF-16 到 UTF-8 转换)
        <i id='6hBu2'><tr id='6hBu2'><dt id='6hBu2'><q id='6hBu2'><span id='6hBu2'><b id='6hBu2'><form id='6hBu2'><ins id='6hBu2'></ins><ul id='6hBu2'></ul><sub id='6hBu2'></sub></form><legend id='6hBu2'></legend><bdo id='6hBu2'><pre id='6hBu2'><center id='6hBu2'></center></pre></bdo></b><th id='6hBu2'></th></span></q></dt></tr></i><div id='6hBu2'><tfoot id='6hBu2'></tfoot><dl id='6hBu2'><fieldset id='6hBu2'></fieldset></dl></div>

          <small id='6hBu2'></small><noframes id='6hBu2'>

              <tbody id='6hBu2'></tbody>
                <legend id='6hBu2'><style id='6hBu2'><dir id='6hBu2'><q id='6hBu2'></q></dir></style></legend>

                  <bdo id='6hBu2'></bdo><ul id='6hBu2'></ul>
                • <tfoot id='6hBu2'></tfoot>
                  本文介绍了JavaScript 中的 UTF-16 到 UTF-8 转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have Base64 encoded data that is in UTF-16 I am trying to decode the data but most libraries only support UTF-8. I believe I have to drop the null bites but I am unsure how.

                  Currently I am using David Chambbers Polyfill for Base64, but I have also tried other libraries such as phpjs.org, none of which support UTF-16.

                  One thing to point out is on Chrome the atob method works with out problem, Firefox I get results described here, and in IE I am only returned the first character.

                  Any help is greatly appreciated

                  解决方案

                  You want to decode UTF-16, not convert to UTF-8. Decoding means that the result is a string of abstract characters. Of course there is an internal encoding for strings as well, UTF-16 or UCS-2 in javascript, but that's an implementation detail.

                  With strings the goal is that you don't have to worry about encodings but just about manipulating characters "as they are". So you can write string methods that don't need to decode input at all. Of course there are many edge cases where this falls apart.

                  You cannot decode utf-16 just by removing nulls. I mean this will work fine for the first 256 code points of unicode, but you will get garbage when any of the other ~110000 characters in unicode are used. You cannot even get the most popular non-ASCII characters like em dash or any smart quotes working.

                  Also, looking at your example, it looks like UTF-16LE.

                  //Braindead decoder that assumes fully valid input
                  function decodeUTF16LE( binaryStr ) {
                      var cp = [];
                      for( var i = 0; i < binaryStr.length; i+=2) {
                          cp.push( 
                               binaryStr.charCodeAt(i) |
                              ( binaryStr.charCodeAt(i+1) << 8 )
                          );
                      }
                  
                      return String.fromCharCode.apply( String, cp );
                  }
                  
                  var base64decode = atob; //In chrome and firefox, atob is a native method available for base64 decoding
                  
                  var base64 = "VABlAHMAdABpAG4AZwA";
                  var binaryStr = base64decode(base64);
                  var result = decodeUTF16LE(binaryStr);
                  

                  Now you can even get smart quotes working:

                  var base64 = "HCBoAGUAbABsAG8AHSA="
                  var binaryStr = base64decode(base64);
                  var result = decodeUTF16LE(binaryStr);
                  //""hello""
                  

                  这篇关于JavaScript 中的 UTF-16 到 UTF-8 转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)

                    <tbody id='8cpOO'></tbody>

                  <small id='8cpOO'></small><noframes id='8cpOO'>

                    1. <tfoot id='8cpOO'></tfoot>
                        <bdo id='8cpOO'></bdo><ul id='8cpOO'></ul>
                        <legend id='8cpOO'><style id='8cpOO'><dir id='8cpOO'><q id='8cpOO'></q></dir></style></legend>

                          <i id='8cpOO'><tr id='8cpOO'><dt id='8cpOO'><q id='8cpOO'><span id='8cpOO'><b id='8cpOO'><form id='8cpOO'><ins id='8cpOO'></ins><ul id='8cpOO'></ul><sub id='8cpOO'></sub></form><legend id='8cpOO'></legend><bdo id='8cpOO'><pre id='8cpOO'><center id='8cpOO'></center></pre></bdo></b><th id='8cpOO'></th></span></q></dt></tr></i><div id='8cpOO'><tfoot id='8cpOO'></tfoot><dl id='8cpOO'><fieldset id='8cpOO'></fieldset></dl></div>