英文:
Unhexlify in Javascript doesn't work with accents
问题
Here's the translated content you requested:
长话短说,我正在从Python发送一个Excel文件到Javascript。为了这样做,我需要将它作为一个字符串发送(虽然这很不规范,但这是我的约束),所以我在Python中将我的Excel文件进行了“hexlify”(或使用.hex(),只要在Javascript中能正常工作都可以)然后发送给Javascript。
with open(EXCEL_TEMPLATE_OUTPUTS, "rb") as f:
encrypted_bytes = f.read()
return encrypted_bytes.hex()
现在的难点是如何在Javascript中将其转换回字节数组,以便我可以创建一个Blob。为此,我使用了下面的函数:
var unhexlify = function(str) {
var bytes = [];
for (var i=0, l=str.length; i<l; i+=2) {
bytes.push(String.fromCharCode(parseInt(str.substr(i, 2), 16)));
}
return bytes;
它运行得相当不错,除了一些字符。例如,“c8”被转换成“È”而不是“\xc8”。因此,我正在重建的文件受损。有没有办法解决这个问题?
编辑:以下是一个突出显示问题的示例:
console.log(String.fromCharCode(parseInt("c8", 16)));
正如你所看到的,它发送的是“È”而不是“\xc8”,而在Python中:
from binascii import hexlify, unhexlify
unhexlify("c8")
# b'\xc8'
bytes.fromhex('c8')
# b'\xc8'
英文:
Long story short, I'm sending an Excel file from Python to Javascript. To do so, I need to send it as a string (very dirty but this is a constraint on my side), and so, I "hexlify" (or use .hex(), any way to do it is ok as long as it works in the Javascript) my Excel file in Python and send it to the Javascript.
with open(EXCEL_TEMPLATE_OUTPUTS, "rb") as f:
encrypted_bytes = f.read()
return encrypted_bytes.hex()
The difficulty now is to convert it back to an array of bytes in the Javascript, so that I can create a Blob. To do so, I'm using the function below:
var unhexlify = function(str) {
var bytes = [];
for (var i=0, l=str.length; i<l; i+=2) {
bytes.push(String.fromCharCode(parseInt(str.substr(i, 2), 16)));
}
return bytes;
It works quite well except for some characters. For example, "c8" is converted into "È" instead of "\xc8". So, the file I'm rebuilding is corrupted. Is there any way to overcome this issue?
EDIT: Here below is an example highlighting the problem:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
console.log(String.fromCharCode(parseInt("c8", 16)));
<!-- end snippet -->
As you can see, it is sending "È" instead of "\xc8", while in Python:
from binascii import hexlify, unhexlify
unhexlify("c8")
# b'\xc8'
bytes.fromhex('c8')
# b'\xc8'
答案1
得分: 1
I have finally found a solution that actually gets rid of String.fromCharCode(). The idea here is to create a Uint8Array from the hex sent from Python:
var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {return parseInt(h, 16)}))
I can then create a blob based on this Uint8Array:
const blob = new Blob([typedArray], { type: "application/octet-stream" });
Thanks @Konrad. You guided me on the right path to find the solution!
英文:
I've finally found a solution that actually get rid of String.fromCharCode(). The idea here is to create a Uint8Array from the hex sent from Python:
var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {return parseInt(h, 16)}))
I can then create a blob based on this Uint8Array:
const blob = new Blob([typedArray], { type: "application/octet-stream" });
Thanks @Konrad. You guided me on the right path to find the solution!
答案2
得分: 0
The error may be in the str.padStart(2, '0')
code during encoding. 或者您可以指定要解码的一些十六进制数。
英文:
maybe the error is in the str.padStart(2,'0')
code during encoding. or you can specify some hex you want to decode
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var hexlify = function(str){
return [...str].map(e =>
e.charCodeAt(0).toString(16)
.padStart(2, '0') // a -> 0a
).join('')
}
var unhexlify = function(str) {
return String.fromCharCode(...
str.match(/.{1,2}/g) // ['68','65','6c','6c','6f']
.map(e => parseInt(e, 16))
)
}
console.log(hexlify('hello'), unhexlify('68656c6c6f')) // 68656c6c6f hello
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论