Javascript 将FileReader.readAsBinaryString转换为Unicode转义字符串

Javascript 将FileReader.readAsBinaryString转换为Unicode转义字符串,javascript,unicode,filereader,Javascript,Unicode,Filereader,文件阅读器。返回UTF-8编码的二进制字符串。如何将数据转换为一系列Unicode转义序列\uxxx?我想我可能已经为您找到了解决方案 在上,我发现了一个将文本转换为Unicode符号的开源项目 我编辑了与项目相关的函数,并创建了一个小函数来处理打开的文件 /* Copyright (C) 2007 Richard Ishida ishida@w3.org This program is free software; you can redistribute it and/or modify

文件阅读器。返回UTF-8编码的二进制字符串。如何将数据转换为一系列Unicode转义序列\uxxx?

我想我可能已经为您找到了解决方案

在上,我发现了一个将文本转换为Unicode符号的开源项目

我编辑了与项目相关的函数,并创建了一个小函数来处理打开的文件

 /*
Copyright (C) 2007  Richard Ishida ishida@w3.org
This program is free software; you can redistribute it and/or modify it under the terms 
of the GNU General Public License as published by the Free Software Foundation; either 
version 2 of the License, or (at your option) any later version as long as you point to 
http://rishida.net/ in your code.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
See the GNU General Public License for more details. http://www.gnu.org/licenses/gpl.html
*/

 function dec2hex(textString) {
     return (textString + 0).toString(16).toUpperCase();
 }

 function convertCharStr2Unicode(textString, preserve, pad) {
     // converts a string of characters to U+... notation, separated by space
     // textString: string, the string to convert
     // preserve: string enum [ascii, latin1], a set of characters to not convert
     // pad: boolean, if true, hex numbers lower than 1000 are padded with zeros
     var haut = 0;
     var n = 0;
     var CPstring = '';
     for (var i = 0; i < textString.length; i++) {
         var b = textString.charCodeAt(i);
         if (b < 0 || b > 0xFFFF) {
             CPstring += 'Error in convertChar2CP: byte out of range ' + dec2hex(b) + '!';
         }
         if (haut != 0) {
             if (0xDC00 <= b && b <= 0xDFFF) {
                 CPstring += dec2hex(0x10000 + ((haut - 0xD800) << 10) + (b - 0xDC00)) + ' ';
                 haut = 0;
                 continue;
             } else {
                 CPstring += 'Error in convertChar2CP: surrogate out of range ' + dec2hex(haut) + '!';
                 haut = 0;
             }
         }
         if (0xD800 <= b && b <= 0xDBFF) {
             haut = b;
         } else {
             if (b <= 127 && preserve == 'ascii') {
                 CPstring += textString.charAt(i) + ' ';
             } else if (b <= 255 && preserve == 'latin1') {
                 CPstring += textString.charAt(i) + ' ';
             } else {
                 cp = dec2hex(b);
                 if (pad) {
                     while (cp.length < 4) {
                         cp = '0' + cp;
                     }
                 }
                 CPstring += '\\u' + cp + ' ';
             }
         }
     }
     return CPstring.substring(0, CPstring.length - 1);
 }

带有文件处理和文本输入的演示:

我想我可能已经为您找到了解决方案

在上,我发现了一个将文本转换为Unicode符号的开源项目

我编辑了与项目相关的函数,并创建了一个小函数来处理打开的文件

 /*
Copyright (C) 2007  Richard Ishida ishida@w3.org
This program is free software; you can redistribute it and/or modify it under the terms 
of the GNU General Public License as published by the Free Software Foundation; either 
version 2 of the License, or (at your option) any later version as long as you point to 
http://rishida.net/ in your code.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
See the GNU General Public License for more details. http://www.gnu.org/licenses/gpl.html
*/

 function dec2hex(textString) {
     return (textString + 0).toString(16).toUpperCase();
 }

 function convertCharStr2Unicode(textString, preserve, pad) {
     // converts a string of characters to U+... notation, separated by space
     // textString: string, the string to convert
     // preserve: string enum [ascii, latin1], a set of characters to not convert
     // pad: boolean, if true, hex numbers lower than 1000 are padded with zeros
     var haut = 0;
     var n = 0;
     var CPstring = '';
     for (var i = 0; i < textString.length; i++) {
         var b = textString.charCodeAt(i);
         if (b < 0 || b > 0xFFFF) {
             CPstring += 'Error in convertChar2CP: byte out of range ' + dec2hex(b) + '!';
         }
         if (haut != 0) {
             if (0xDC00 <= b && b <= 0xDFFF) {
                 CPstring += dec2hex(0x10000 + ((haut - 0xD800) << 10) + (b - 0xDC00)) + ' ';
                 haut = 0;
                 continue;
             } else {
                 CPstring += 'Error in convertChar2CP: surrogate out of range ' + dec2hex(haut) + '!';
                 haut = 0;
             }
         }
         if (0xD800 <= b && b <= 0xDBFF) {
             haut = b;
         } else {
             if (b <= 127 && preserve == 'ascii') {
                 CPstring += textString.charAt(i) + ' ';
             } else if (b <= 255 && preserve == 'latin1') {
                 CPstring += textString.charAt(i) + ' ';
             } else {
                 cp = dec2hex(b);
                 if (pad) {
                     while (cp.length < 4) {
                         cp = '0' + cp;
                     }
                 }
                 CPstring += '\\u' + cp + ' ';
             }
         }
     }
     return CPstring.substring(0, CPstring.length - 1);
 }

演示文件处理和文本输入:

此函数将转义非ascii字符并将unicode转换回\uhhh

function ascii(str) {
  var s = ""

  for (var i = 0, len = str.length; i < len; i++) {
    var n = str.charCodeAt(i);
    if (n >= 32 && n <= 126) {
      // printable ASCII
      s += str.charAt(i);
    } else {
      // unicode escape everything else
      n = n.toString(16);
      n = "0000".substr(n.length) + n;
      s+= "\\u" + n;
    }
  }
  return s
}

此函数将转义非ascii字符并将unicode转换回\uhhh

function ascii(str) {
  var s = ""

  for (var i = 0, len = str.length; i < len; i++) {
    var n = str.charCodeAt(i);
    if (n >= 32 && n <= 126) {
      // printable ASCII
      s += str.charAt(i);
    } else {
      // unicode escape everything else
      n = n.toString(16);
      n = "0000".substr(n.length) + n;
      s+= "\\u" + n;
    }
  }
  return s
}
FileReader.readAsBinaryString不在文件中

使用Blob:

在下面的代码片段中,我得到了一个图像,但是您可以得到任何二进制文件类型

var fileDisplayArea=document.getElementById'fileDisplayArea'; var testFrame=document.getElementById'testFrame'; window.URL=window.URL | | window.webkitURL;//注意供应商前缀。 var xhr=新的XMLHttpRequest; //日志'has CORS',带有xhr中的凭证; xhr.open'GET','http://i.imgur.com/Ar1SBEH.png",对,; xhr.responseType='blob'; xhr.onload=函数e{ 如果this.status==200{ var blob=this.response;//成为blob类型 console.logblob; var url=window.url.createObjectURLblob; testFrame.src=url; var img=document.createElement'img'; img.onload=函数e{ window.URL.revokeObjectURLimg.src;//请自己清理。 }; img.src=window.URL.createObjectURLblob; fileDisplayArea.appendChildimg; } }; xhr.send; FileReader.readAsBinaryString不在文件中

使用Blob:

在下面的代码片段中,我得到了一个图像,但是您可以得到任何二进制文件类型

var fileDisplayArea=document.getElementById'fileDisplayArea'; var testFrame=document.getElementById'testFrame'; window.URL=window.URL | | window.webkitURL;//注意供应商前缀。 var xhr=新的XMLHttpRequest; //日志'has CORS',带有xhr中的凭证; xhr.open'GET','http://i.imgur.com/Ar1SBEH.png",对,; xhr.responseType='blob'; xhr.onload=函数e{ 如果this.status==200{ var blob=this.response;//成为blob类型 console.logblob; var url=window.url.createObjectURLblob; testFrame.src=url; var img=document.createElement'img'; img.onload=函数e{ window.URL.revokeObjectURLimg.src;//请自己清理。 }; img.src=window.URL.createObjectURLblob; fileDisplayArea.appendChildimg; } }; xhr.send; FileReader.readAsBinaryString已弃用-请改用。这允许您使用以下两种方法之一将输入字符串转换为转义unicode字符:

方法1 这将使用ArrayBuffer和其上的Uint8Array视图。在下面的演示中,假定缓冲区已预加载,但提供了一些虚拟数据

var buffer=new Uint8Array[0x20,0xac,0x2b,0x08],//大端字节格式 pos=0,txt=0; //每字节迭代缓冲区字节并生成字符串: whilepos 方法1 这将使用ArrayBuffer和其上的Uint8Array视图。在下面的演示中,假定缓冲区已预加载,但提供了一些虚拟数据

var buffer=new Uint8Array[0x20,0xac,0x2b,0x08],//大端字节格式 pos=0,txt=0; //每字节迭代缓冲区字节并生成字符串: whilepos有人知道如何将数据从字节转换为转义unicode字符吗?请给我们一个工作示例,我无法用ajax再现您想要的效果,我看到的只是正方形,而不是转义码。也就是说,你可以使用arraybuffer,如果你有一个二进制响应,你可以使用URL.createObjectURL将它转换成一个支持ajax的东西,在这里你可以使用上面的技巧。有人知道如何将数据从字节转换成转义的unicode字符吗?请给我们一个工作示例,我无法用ajax重现您想要的效果,我看到的只是方块, 不是转义码。也就是说,您可以得到一个arraybuffer,如果您有一个二进制响应,您可以使用URL.createObjectURL将其转换为一个支持ajax的东西,在这里您可以执行上述技巧。
xhr.responseType = 'blob';