构造Blob时的字符串编码 [英] String encoding when constructing a Blob

查看:209
本文介绍了构造Blob时的字符串编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道JavaScript字符串通常编码为具有每个字符至少两个字节的编码(UTF-16或UCS-2)。

I know that JavaScript strings are usually encoded with an encoding taking at least two bytes per character (UTF-16 or UCS-2).

但是,当构造Blob时,似乎使用了不同的编码,因为当我将它读为 ArrayBuffer 时,返回的缓冲区为3欧元符号。

However, when constructing a Blob, a different encoding appears to be used because when I read it as ArrayBuffer, the length of the returned buffer is 3 for an Euro sign.

var b = new Blob(['€']);


推荐答案

根据 W3C ,它是 UTF-8 编码。

演示:

// Create a Blob with an Euro-char (U+20AC)
var b = new Blob(['€']);
var fr = new FileReader();

fr.onload = function() {
  ua = new Uint8Array(fr.result);
  // This will log "3|226|130|172"
  //                  E2  82  AC
  // In UTF-16, it would be only 2 bytes long
  console.log(
    fr.result.byteLength + '|' + 
    ua[0]  + '|' + 
    ua[1] + '|' + 
    ua[2] + ''
  );
};
fr.readAsArrayBuffer(b);

JSFiddle

Play with that on JSFiddle.

这篇关于构造Blob时的字符串编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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