JavaScript将图片字节转换为十六进制

3
我尝试通过画布将本地图像文件转换为字符串。
imgData = canvas.toDataURL("image/jpeg");

但它只返回一个Base64编码的字符串。

有没有什么方法将图像二进制转换为十六进制字符串,例如:

0x310000700008000400efbeee3a851a54...

与 Perl/Ruby 中 unpack('H*') 函数类似。


b64字符串出了什么问题?如果解码它会发生什么?那不会给你二进制吗? - cegfault
我觉得他可能是指他想要类似于Perl中的pack/unpack这样的函数。 - shouya
2个回答

0

这是来自https://developer.mozilla.org/zh-CN/docs/Web/API/WindowBase64/Base64_encoding_and_decoding的内容

在 JavaScript 中,有两个函数分别用于解码和编码 base64 字符串:

atob()

btoa()

var encodedData = window.btoa("Hello, world"); // encode a string

var decodedData = window.atob(encodedData); // decode the string

encodedData = window.btoa("011110000100101"); //RESULT is "MDExMTEwMDAwMTAwMTAx"

或者(在node.js中)

fs = require('fs');
imgBuffer = fs.readFileSync('public/images/my_image_location.gif');
imgHex = imgBuffer.toString('hex');
console.log(imgHex)

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接