在Javascript中,如何解码解码字符串包含二进制(例如非UTF-8)数据的字符串?

时间:2018-05-25 21:41:16

标签: javascript binary base64 decode encode

我有一个表示二进制数据的base64字符串(例如,在解码之后不是一个很好的可读字符串或者UTF-8数据,否则这个答案对我有帮助 - Using Javascript's atob to decode base64 doesn't properly decode utf-8 strings)。我的JS是

var byteCharacters = atob(base64str);

导致

InvalidCharacterError: Failed to execute 'atob' on 'WorkerGlobalScope': The string to be decoded contains characters outside of the Latin1 range.

如何解码非ASCII(甚至是UTF-8数据)的基本64字符串?

编辑:以下是我所谈论的字符串类型的示例。使用Mac,我可以base64编码一个像这样的文件

openssl base64 -in ~/Downloads/star.jpg -out ~/Downloads/star.base64

这是生成的base64字符串。如果你可以用JS工具解码这个,你就解决了这个问题。通过做" cat myfile.base64"得到了以下内容。所以希望" cat"或者我的切割和粘贴并没有在这里引入任何奇怪的角色。

iVBORw0KGgoAAAANSUhEUgAAAQMAAADCCAMAAAB6zFdcAAAAulBMVEUNKlK+vr7/
///FxMMAJU/BwcAAFUgAIU1BUGoAHEsAIE0AI04AHkyztbgAGEkJKFEAEEXU2N48
T27JzNMAAEGGjZhye4urrrKkqa6doqgRLlYAE0c2SGaQlp8AAD99hZJfaHyYnKMb
NFkACkNKWXHs7vFncYPh5OgmPF4UM1tWY3ior7uSmqmAh5ROXHS7wcotQGGKlKR1
gZWqsb13f4xbaYNueo9faHsAADadpbK/xM0iPmXo6+5we5BqJza2AAAVW0lEQVR4
nO1dC3vaOLP2RcLYsiVCuF+ScAkh2YaSJm1P9+v+/791NCPJFzAFHFpwnXl2nwZs
I3msd+ad0Ui2rA/5kA/5kA+5GJmSc/fg7BI8r8W5+3BmEWG08M7diTMLabq98Nyd
OLMEt9SdVhwMw8h2Kw4GMnJt2uPn7sZZJbyltm1X2jMIryt1UG0wIBQkGGrn7sgZ
RXoFOpEDocqewe/a7nxC3UVw7p6cTUid2hFZuLTnn7srZxOAwphPpUWoLhgakiCt
GO/T6noG8Ap2YHkAhqrSp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uHMdor/EoHspF89gTHUNHH66DAZhq7bLscyViVnte7xOeR
ZuoOwlQIAekRb68OoO6I7TgWoaKP1kFUqHK+nd9HOZJ3vIDYXSWqTiZCEIhp66JZ
6y90AJuoWDsQpx3rsTpwC22hkD+Z5AIbyR8j2t4rSe4A9/ry4uRXXGP4Cx3guN2c
vNSX62nvo8dBsUImkjPf5/ZwSIU5FkHG76lmkjmICF2ZYTOJ/9ytAx0CequcRszi
7yN1UHi9NBGTjYlnd6KtHr+jGy253WXG6HgaDHofjzCmXMZu7tIBMDx1RjjfjP6S
RMBeHaTz9lD2XkwFUALx1E2n7qNFYvhHE5rJ6t9uFCebSQidNQvjEMLYzVwdQFy6
jh8Zm44zjdBxUpqwNRWY0oGUaCFDJqN2913bzjF/Pu7qco/xU5jalkRS8Tddo+La
/c50c+0i04Gtji2FJu403jA5z6hEvXadp3Qp+LLTj3Qjvbd1UqJC6lsXt1VL4bg/
mfTrvsdHbbyUTt67dT8Lw8GoXl+LwGfZ25TfeI/y0OjR83P2bCHMk8JMtCY8lNS7
FPQ3aQn8zepO4fnBYC0bmcowUGQObIq5MpCRBJYuEez6iJykYlRVmOUcgHhOHss3
uRtVbGKzqi3n5bX5v4OVZVvHNi/NvVi8pyDtQz7kQz7kQ6op/w/Zx845GtASrQAA
AABJRU5ErkJggg==

4 个答案:

答案 0 :(得分:0)

上次修改应该对2015年相关 SO answer

中的这句话进行更多宣传
  

原始解决方案(已弃用)

当提及 atob,btoa,base64js和其他库功能时:使用库节省您的时间并弃用atob和btoa

尝试使用您的数据base64.js。toByteArray。点击图书馆Github自述文件的链接:它说它完全符合您的要求,嗯,只有来自和来自。

在Chrome 66.0.3359.181上使用您的数据base64js.min.js成功测试了@ 1.3.0

使用Nodejs @ 4.4.7

中的数据成功测试了@ 1.3.0

引自自述文件:

  

base64js有三个公开的函数,byteLength,toByteArray和fromByteArray,它们都只接受一个参数。

  1. byteLength - 采用base64字符串并返回字节数组的长度
  2. toByteArray - 获取base64字符串并返回字节数组
  3. fromByteArray - 采用字节数组并返回base64字符串
  4. 如果您可以访问它,我还会根据它应该具有的任何文件类型/结构来测试原始二进制数据。

    您是否针对base64测试了base64字符串,首先,它是否是正确的base64字符串?

    公平地说明代码Example链接以及Github仓库中的更多信息,当作为脚本包含在客户端中时,使用:

    var byteArray = base64js.toByteArray(myBase64Str)
    

    在Node中,您可以:

    var fs = require('fs')
    var base64js =  require('base64-js')
    fs.readFile('pathToMyBase64File', 'utf8', (err, data) => { // here 'utf8' is mandatory
      if (err) throw err;
      var byteArray = base64js.toByteArray(data)
      console.log(base64js.byteLength(data) == byteArray.length)
    })
    

    重要的是,myBase64File应使用utf-8 w / BOM进行编码,否则无效。

    P.S。 :我不是这个图书馆的contributor,只是每个人都知道的好工具,LICENSE MIT。

答案 1 :(得分:0)

正如我在评论中已经提到的,问题在于编码。 您的共享base64字符串中包含许多换行符('\ n')。 为了用户可读性,添加了新行(通过 cat )。但它们在解码时会引起麻烦。 如果您在删除字符串中的所有'\ n'并尝试atob()之后尝试,它应该正常工作。

获取不带空格的base64字符串或'\ n'

在Linux中

$ base64 -w 0 ~/Downloads/star.jpg >~/Downloads/star.base64

在Mac中,您可以在openssl中尝试 -A 选项。

$ openssl base64 -A -in ~/Downloads/star.jpg -out ~/Downloads/star.base64

请使用生成的字符串尝试atob()(直接从文件复制而不是从cat输出)。

注意:我不是Mac用户

答案 2 :(得分:-1)

  

如何解码非ASCII(甚至是UTF-8数据)的基本64字符串?

你不能。 Base64编码仅使用ASCII字符(A-Za-z0-9+/=)。如果您的字符串包含非ASCII字符,则它不是Base64数据;它完全是别的东西,atob()无法做任何事情。

最有可能的是字符串已被解码。

答案 3 :(得分:-1)

您可以尝试以下代码:

function decode_base64(s) {
  var b=l=0, r='',
  m='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  s.split('').forEach(function (v) {
    b=(b<<6)+m.indexOf(v); l+=6;
    if (l>=8) r+=String.fromCharCode((b>>>(l-=8))&0xff);
  });
  return r;
}  

然后你可以创建一个可以完成工作的异步函数:

function decode_base64_async(s, cb) {
  setTimeout(function () { cb(decode_base64(s)); }, 0);
}  

P.S:我没有为非ASCII字符检查此代码,因此您需要尝试