在JavaScript中,Unhexlify不支持带有重音符号的字符。

huangapple go评论72阅读模式
英文:

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, &quot;rb&quot;) 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&lt;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(&quot;c8&quot;, 16)));

<!-- end snippet -->

As you can see, it is sending "È" instead of "\xc8", while in Python:

from binascii import hexlify, unhexlify
unhexlify(&quot;c8&quot;)
# b&#39;\xc8&#39;
bytes.fromhex(&#39;c8&#39;)
# b&#39;\xc8&#39;

答案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: &quot;application/octet-stream&quot; });

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,&#39;0&#39;) 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 =&gt; 
       e.charCodeAt(0).toString(16)
         .padStart(2, &#39;0&#39;) // a -&gt; 0a
      ).join(&#39;&#39;)
}
var unhexlify = function(str) {
   return String.fromCharCode(...
     str.match(/.{1,2}/g) // [&#39;68&#39;,&#39;65&#39;,&#39;6c&#39;,&#39;6c&#39;,&#39;6f&#39;]
     .map(e =&gt; parseInt(e, 16))
     )
}
console.log(hexlify(&#39;hello&#39;), unhexlify(&#39;68656c6c6f&#39;)) // 68656c6c6f hello

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月13日 14:05:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定