简体   繁体   English

Javascript window.atob - > HEX - 结果与预期不同

[英]Javascript window.atob -> HEX - different result than expected

I have a BMP image and I need to send it to a device via TCP / IP . 我有一个BMP图像,我需要通过TCP / IP将其发送到设备。 We already have the C library in our company, which can handle this, but I need to do it in JavaScript. 我们公司已经有了C库,可以处理这个,但我需要在JavaScript中完成。 Unfortunately, I don't have access to the source code of the library, nor to the device system. 不幸的是,我无法访问库的源代码,也无法访问设备系统。

This is a Base64 string of a sample image (a black&white checkmark): 这是样本图像的Base64字符串(黑白复选标记):

Qk2+AAAAAAAAAD4AAAAoAAAAIAAAACAAAAABAAEAAAAAAIAAAADEDgAAxA4AAAAAAAAAAAAAAAAAAP///wD//////+H////A////gH///wB///4AP//8AD//+AAf//AAD//gAA//yAAH/5wAB/+8EAP/fjAB/394Af+++AD/vPwAf8P+AH///gA///9AH///v5///7/P///fx///7+P//+/z///38f//+/n///38///+/f///v3///97////hw== Qk2 + AAAAAAAAAD4AAAAoAAAAIAAAACAAAAABAAEAAAAAAIAAAADEDgAAxA4AAAAAAAAAAAAAAAAAAP /// WD ////// + H //// ////甲///的gH WB /// 4AP // // 8AD + AAF // // AAD GAA // yAAH / 5wAB / + 8EAP / fjAB / 394Af +++ AD / vPwAf8P + AH /// GA /// 9AH /// V5 /// 7 / P /// FX /// 7 + P + // / Z / // // 38F + / N /// 38 /// + / F /// V3 /// 97 //// HW ==

I use window.atob and I am encoding it into HEX. 我使用window.atob ,我将其编码为HEX。 I use this JS function: 我用这个JS函数:

function toHex(str) {
    var result = '';
    for (var i = 0; i < str.length; i++) {
        result += str.charCodeAt(i).toString(16);
    }
    return result;
}
var str = window.atob(base64img);
var result = toHex(str);

It gives me this result, which is almost expected result: 它给了我这个结果,这几乎是预期的结果:

424dbe00000003e0002800020000200001010000080000c4e00c4e00000000000000ffffff0ffffffffffe1ffffffc0ffffff807fffff07ffffe03ffffc03ffff801ffff00fffe00fffc807ff9c07ffbc103ff7e301ff7f781ffbef80ffbcfc07fc3fe07ffffe03fffff401fffffbf9fffffbfcfffffdfc7ffffefe3ffffeff3fffff7f1fffffbf9fffffdfcfffffefdfffffefdffffff7bffffff87 424dbe00000003e0002800020000200001010000080000c4e00c4e00000000000000ffffff0ffffffffffe1ffffffc0ffffff807fffff07ffffe03ffffc03ffff801ffff00fffe00fffc807ff9c07ffbc103ff7e301ff7f781ffbef80ffbcfc07fc3fe07ffffe03fffff401fffffbf9fffffbfcfffffdfc7ffffefe3ffffeff3fffff7f1fffffbf9fffffdfcfffffefdfffffefdffffff7bffffff87

The library sends the exact same image correctly (the device accepts the message). 库正确发送完全相同的图像(设备接受该消息)。 This is how it looks like (copied from log): 这是它的样子(从日志中复制):

be00424dbe000000000000003e000000280000002000000020000000010001000000000080000000c40e0000c40e0000000000000000000000000000ffffff00ffffffffffe1ffffffc0ffffff807fffff007ffffe003ffffc003ffff8001ffff0000fffe0000fffc80007ff9c0007ffbc10101003ff7e3001ff7f7801ffbef800ffbcfc007fc3fe007ffffe003fffff401fffffbf9fffffbfcfffffdfc7ffffefe3ffffeff3fffff7f1fffffbf9fffffdfcfffffefdfffffefdffffff7bffffff87 be00424dbe000000000000003e000000280000002000000020000000010001000000000080000000c40e0000c40e0000000000000000000000000000ffffff00ffffffffffe1ffffffc0ffffff807fffff007ffffe003ffffc003ffff8001ffff0000fffe0000fffc80007ff9c0007ffbc10101003ff7e3001ff7f7801ffbef800ffbcfc007fc3fe007ffffe003fffff401fffffbf9fffffbfcfffffdfc7ffffefe3ffffeff3fffff7f1fffffbf9fffffdfcfffffefdfffffefdffffff7bffffff87

So this is what I need to get from Base64 in my JavaScript. 所以这就是我在JavaScript中从Base64获得的东西。 Is it even possible? 它甚至可能吗? Or am I missing something? 或者我错过了什么?

Documentation for the library says the image has to be 2B binary data (Little Endian). 该库的文档说该图像必须是2B二进制数据(Little Endian)。 I don't understand it. 我不明白。 Should I encode the image any other way? 我应该以任何其他方式编码图像吗?

One option would be encode each byte separately to ensure correct endianness. 一种选择是分别对每个字节进行编码以确保正确的字节序。

 img = "Qk2+AAAAAAAAAD4AAAAoAAAAIAAAACAAAAABAAEAAAAAAIAAAADEDgAAxA4AAAAAAAAAAAAAAAAAAP///wD//////+H////A////gH///wB///4AP//8AD//+AAf//AAD//gAA//yAAH/5wAB/+8EAP/fjAB/394Af+++AD/vPwAf8P+AH///gA///9AH///v5///7/P///fx///7+P//+/z///38f//+/n///38///+/f///v3///97////hw==" str = atob(img) buf = [] function hex(str, pos) { return ('000' + (str.charCodeAt(pos) || 0).toString(16)).substr(-2); } for (var i = 0; i < str.length; i+= 4) { buf.push(hex(str, i+2)); buf.push(hex(str, i+3)); buf.push(hex(str, i+0)); buf.push(hex(str, i+1)); } console.log(buf.join('')) 

This doesn't match your desired output exactly, are you sure it corresponds to the given base64 string? 这与您想要的输出完全不符,您确定它对应于给定的base64字符串吗?

On the other side, your initial output looks better, 424d are correct bytes to start a BMP file ( BM signature), while be00 are not. 另一方面,您的初始输出看起来更好, 424d是启动BMP文件( BM签名)的正确字节,而be00则不是。

This is a Base64 string of a sample image (a black&white checkmark)... 这是一个样本图像的Base64字符串(黑白复选标记)......
It gives me this result, which is almost expected result: 它给了我这个结果,这几乎是预期的结果:

 424dbe00000003e0002800020000200001010000080000c4e00c4e00000000000000ffffff0ffffffffffe1ffffffc0ffffff807fffff07ffffe03ffffc03ffff801ffff00fffe00fffc807ff9c07ffbc103ff7e301ff7f781ffbef80ffbcfc07fc3fe07ffffe03fffff401fffffbf9fffffbfcfffffdfc7ffffefe3ffffeff3fffff7f1fffffbf9fffffdfcfffffefdfffffefdffffff7bffffff87 

What do you mean by "almost expected result" ?, those bytes make the image below (save as .bmp ). 什么是“几乎预期的结果”是什么意思?,这些字节使下面的图像(另存为.bmp )。
Your expected "a black&white checkmark" image : 您预期的"a black&white checkmark"图片:

PS: PS:

The correct .bmp file has 190 bytes. 正确的.bmp文件有190个字节。 Your library version (copied from logs) gives result of 194 bytes. 您的库版本(从日志中复制)给出了194个字节的结果。

The library's version of bytes has a beginning 2-byte short for file size. 库的字节版本的文件大小为2字节的short
That is BE 00 ( = 190 when read as endian of 00 BE ) followed by rest of Bitmap file bytes (190 total bytes). BE 00 (当读为00 BE endian时为= 190 ),然后是其余的Bitmap文件字节(总共190个字节)。 This makes total of 190 bytes + the 2 bytes of short . 这使得总共190个字节+ 2个字节的short Then it adds these two mysterious 10 and 10 bytes starting at position 114. Grand total of 194 bytes. 然后它从位置114开始添加这两个神秘的1010个字节。总共194个字节。

To me... the library is corrupting the already fine image bytes, but you say the device accepts it? 对我来说......库正在破坏已经很好的图像字节,但是你说设备接受它了吗?
Does it accept the "almost expected result" hex bytes too? 它是否也接受“几乎预期的结果”十六进制字节?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM