Based on this thread I tried to create a blob with utf 32 encoding and BOM of FF FE 00 00(UTF-32LE representation) as follows:
var BOM = new Uint8Array([0xFF,0xFE,0x00,0x00]);
var b = new Blob([ BOM, "➀➁➂ Test" ]);
var url = URL.createObjectURL(b);
open(url);
But the file content gets corrupted and gets changed to a different language. What is the correct way to convert a string to a file with utf-32le format?
Edit: Im trying this in browser environment
Note: I'm assuming you're doing this in a browser, since you used Blob and Node.js only recently got Blob support, and you referenced a question doing this in a browser.
You're just setting the BOM, you're not handling converting the data. As it says in MDN's documentation, any strings in the array will be encoded using UTF-8. So you have a UTF-32LE BOM followed by UTF-8 data.
Surprisingly (to me), the browser platform doesn't seem to have a general-purpose text encoder (
TextEncoder
just encodes UTF-8), but JavaScript strings provide a means of iterating through their code points (not just code units) and getting the actual Unicode code point value. (If those terms are unfamiliar, my blog post What is a string? may help.) So you can get that number and convert it into four little-endian bytes.DataView
provides a convenient way to do that.Finally, you'll want to specify the charset in the blob's MIME type (the BOM itself isn't sufficient). I expected
text/plain; charset=UTF-32LE
to work, but it doesn't, at least not in Chromium browsers. There's probably some legacy reason for that.text/html
works (on its own), but we may as well be specific and dotext/html; charset=UTF-32LE
.See comments:
Beware, though, that the specification "prohibits" browsers from supporting UTF-32 (either LE or BE) for HTML:
You might be better off with one of the UTF-16s, given that you're using
window.open
to open the result. (For downloading, UTF-32 is fine if you really want a UTF-32 file.)Sadly, Stack Snippets won't let you open a new window, but here's a full example you can copy and paste to run locally: