【问题标题】:Convert array into hexadecimal file将数组转换为十六进制文件
【发布时间】:2017-09-28 10:39:46
【问题描述】:

我想创建一个可以保存/下载的 hex 文件。

结果应该是一个十六进制格式的文件,像这样:

5350 4401 011a 000a 0900 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
00f8 ff1e ffff ffff b9ff c03b e6bf fbd8
bffb bccc 671c fdee eefd eefe ec6f 3800
0fc0 0003 c000 0180 0f00 1c00 003e 0003
c1e0 07ff f006 3e30 05dd d003 dde0 07be
f00f 6378 0edd b80e 1c38 0f1c 7877 9cf7
7bdd ef7d dddf 1edd bc0f 1c78 07c1 f003
dde0 01be c000 1c00 6f00 1013 4340 3055
6208 181b 4b48 385d 6a72 7678 7b7e 8087
898c 9098 0713 1444 4337 5766 0f1b 1c4c
4b3f 5f6e 7577 7a7d 7f86 888b 8e97 9f02
0610 1006 0205 0502 0610 1006 0205 0503
0204 0304 0503 0303 0202 8080 8080 8080
9090 8080 8080 8080 9090 8080 8080 8080
8080 8080 80

我已经尝试过这个解决方案:Javascript: how to convert hex data to binary and write it into a file 但似乎卡在某个地方。

我的代码

  var bytes = new Array(122, 13, 14, 9, 255); // integer values

  var ab = new ArrayBuffer(bytes.length); //bytes is the array with the integer
  var ia = new Uint8Array(ab);

  for (var i = 0; i < bytes.length; i++) {
    ia[i] = bytes[i];
  }

  save_file(ia,"hexfile.bin");

  save_file(data, filename)
  {

      var file = new Blob([data], {type: "octet/stream"});
      if (window.navigator.msSaveOrOpenBlob) // IE10+
          window.navigator.msSaveOrOpenBlob(file, filename);
      else { // Others
          var a = document.createElement("a"),
                  url = URL.createObjectURL(file);
          a.href = url;
          a.download = filename;
          document.body.appendChild(a);
          a.click();
          setTimeout(function() {
              document.body.removeChild(a);
              window.URL.revokeObjectURL(url);  
          }, 0); 
      }
  }

保存文件有效,但内容不是我所期望的。 感谢您的帮助,谢谢!

【问题讨论】:

  • 您实际得到的结果是什么?意识到没有像十六进制格式的文件这样的东西,这就是在十六进制编辑器中查看二进制文件的方式,除非您的意图是实际制作一个具有该格式每个字节的十六进制值的文本文件

标签: javascript file hex


【解决方案1】:

结果应该是一个十六进制格式的文件,像这样:

十六进制只是文件/缓冲区中内容的直观表示。内容本身存储为字节(十六进制范围 [0x00, 0xff],十进制范围 [0, 255] 等)。

现在您正在根据包含实际字节的数组长度创建一个空数组:

var ab = new ArrayBuffer(bytes.length);  // will be empty
var ia = new Uint8Array(ab);             // therefor also empty

直接转换为:

var ia = new Uint8Array(bytes);          // Array converted to typed array incl. content

现在使用正确的 mime 类型将其传递给 Blob 或 File 对象(并不是说它太重要,因为它与浏览器如何处理文件有关,而不是更改内容 - 未知的 mime 类型会处理方式如下):

var file = new Blob([ia], {type: "application/octet-stream"});

或者如果你想要一个文件名:

var file = new File([ia], "hexfile.bin", {type: "application/octet-stream"});

然后将其用作(或使用您已有的功能以获得更好的兼容性):

document.location = URL.createObjectURL(file);

示例(在十六进制编辑器中保存并打开结果,其中应包含 0x00 到 0x0f 范围内表示的字节):

var bytes = Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
var ia = new Uint8Array(bytes);
var file = new File([ia], "hexfile.bin", {type: "application/octet-stream"});
document.location = URL.createObjectURL(file);

【讨论】:

  • 感谢 K3N 花时间以如此好的方式解释这一点,这非常有帮助。原来我以前有过这个,但忽略了一个小细节,这让我觉得我遵循了错误的方法:文件,当在例如打开时Sublime,以 UTF-8 格式打开。有没有办法确保文件被编码为十六进制?再次感谢您的帮助和解决方案!
  • 我感觉像 Sublime 这样的编辑器会根据数据数量来决定编码。
  • 这将取决于内容。此处的二进制转换不会影响编码,但如果稍后将其视为文本,则可能会。 Sublime 可能会在开头看到(或看不到)BOM marker,或者输入可能是错误的字节顺序(如果来自 UTF16 等)。如果您主要处理二进制文件的文本输入,则可以考虑使用 TextEncoder
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-26
  • 1970-01-01
  • 2010-10-16
  • 2013-05-31
相关资源
最近更新 更多