UTF-16在JavaScript UTF-8的转换 [英] UTF-16 to UTF-8 conversion in JavaScript

查看:322
本文介绍了UTF-16在JavaScript UTF-8的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有恩的Base64的是UTF-16我试图去code中的数据,但大多数图书馆只支持UTF-8 codeD数据。我相信我有砸空咬,但我不能确定如何。

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.

目前我使用的大卫Chambbers填充工具为Base64的,但我也尝试过其他的库,作为 phpjs.org ,其中没有支持UTF-16。

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.

有一点需要指出的是在Chrome的ATOB方法适用于出的问题,火狐我得到的结果描述的此处,并在IE中我只返回的第一个字符。

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.

任何帮助是极大AP preciated

Any help is greatly appreciated

推荐答案

您想取消code UTF-16,不会转换为UTF-8。译码装置,其结果是抽象字符的字符串。当然有字符串的内部编码为好,UTF-16或JavaScript中UCS-2,但是这是一个实现细节。

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.

通过串的目标是,你不必担心编码,但只是操作字符,因为它们。所以,你可以写一个不需要去code输入在所有的字符串方法。当然也有很多优势情况下,这个分崩离析。

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.

您不能去code UTF-16只是通过删除空值。我的意思是这将工作的罚款单code第一次256 code点,但你会得到
当任何单code中的另一〜110000字符均采用垃圾。你甚至不能得到最流行的非ASCII字符,如
破折号或任何智能引号工作。

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.

此外,看着你的榜样,它看起来像UTF-16LE。

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""

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆