-1

I'm trying to get a blob for UTF-16 (I think). Here's the code. I'm not including the full string because it is too long.

  const creatBlobUrl = (str) => {
  console.log(str) 
 //?PNG\r\n\u001a\n\u0000\u0000\u0000\rIHDR\u0000\u0000\b$\u0000\u0000\u00016\b\u0006\u0000\u0000\u0000???\u0000\u0000\fliCCPICC Profile\u0000\u0000H??W\u0007XS?\u0016?[????\u0002?H\t?\tҫ?\u0010Z\u0004\u0001????\u0004\u0012J?\tAņ????E\u0014+?*??Z\u0000YT?^\u0016??\u0017\u000b*+?bAQTބ\u0004t?W?w?o?̙??;s?\u001d\u00004{?\u0012I.?\u0005@?8_\u001a\u001f\u0011?\u001c???$u\u0000\u0002 \u0001\n0\u0006\b?'????\u0001?????&@\u0014?5'\u0005??????\u00052\u001e\u0000?x?3?2^\u001e??\u0000?\u001bx\u0012i>\u0000D??rj?D?? ֕?\u0000!^??YJ?K?3??i?&1?\r?\u0015\u0000Ԩ\\?4\u000b\u0000??P?,?eA\u001e??\u0010???\"1\u0000??!\u000e?\t?|?\u0015?\u000f?˛??\u0015\u0010?A{\t?0\u001e??\u001dg??3???ܬ!??k@?BE2I.w??Y??-y??A\u001f6?Q???xE????s&G)0\u0015?.qFL???\u0010???ʺ\u0003?R???$?=j̓?a?" 
    const blob = new Blob([str], {
      type: 'image/png',
    });

    return URL.createObjectURL(blob);
  };

First of all, is this even UTF-16. Second what am I doing wrong? BTW, my goal is to pass this url into an anchor tag

Eric Grossman
  • 219
  • 1
  • 4
  • 9
  • I tried doing to new Blob([atob(str)]) thing, but that didn't work either. – Eric Grossman Jun 11 '21 at 00:12
  • Does this answer your question? [Blob URL with UTF-16LE encoding](https://stackoverflow.com/questions/27318715/blob-url-with-utf-16le-encoding) – thshea Jun 11 '21 at 00:17
  • 1
    No this does not look like UTF16 to me. It looks like a PNG – Evert Jun 11 '21 at 00:19
  • 1
    It's not even a PNG, this is the **result** of reading the binary data of a PNG file as UTF-8 text. You already lost information in a way it can't be retrieved. For instance, the first byte is now 0x3f (?), while it was 0x89. This is because this byte is not part of UTF-8, and it got mapped to a replacement character. A lot of such bytes may appear in your png data: it's already corrupted. You need to go a few steps back, when whatever did create that string, and making it return directly a Blob instead of a string. Without knowing what did generate that string, we can't help more. – Kaiido Jun 11 '21 at 01:20
  • Where are you seeing 0x3f in the string? Is that equal to u0001a? – Eric Grossman Jun 11 '21 at 02:04
  • The character `?` in UTF8 (and many other encodings based on ASCII) is the byte 0x3f. `\u001a` is 0x1a, and this one is correct at this position. – Kaiido Jun 11 '21 at 03:24

1 Answers1

0

Figured it out. @Kaiido you were right. I was in fact losing data over http. I had to encode the data to base64 on the backend before I sent it over http, and then decode it on the front end with something like: https://stackoverflow.com/a/35713609/11477406

Eric Grossman
  • 219
  • 1
  • 4
  • 9