javascript - 将数组转换为十六进制文件

标签 javascript file 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); 
      }
  }

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

最佳答案

The result should be a file in hex format, like this:

十六进制只是文件/缓冲区中内容的直观表示。内容本身存储为字节(十六进制范围 [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);

关于javascript - 将数组转换为十六进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46467070/

相关文章:

javascript - Passport deserializeUser() 永远不会通过 Axios http 请求调用

c - C 文件中的位数不均匀

python - 为什么 readline() 不返回原始文本?

c# - 替换字符串c#中的控制字符

c# - 从问题背后的代码调用JS函数

javascript - Facebook 喜欢按钮不会回到固定 div 上

c - 为什么 fread() 在一串 0 处停止?

指向 union 存储的字符指针

vb.net - 在vb.NET中替换文件中的 'bel'字符

javascript - 无法读取 null 的属性 'clientHeight' - 工作 JSfiddle 教程在其他地方不起作用?