I am attempting to manipulate some system variables used by a program using Dart. I have encountered the problem of dart's utf package being discontinued, and I have not found any way to encode to UTF 16 Little Endian for a File.write. Is there a library that can do a byte to UTF 16 LE conversion in Dart? I would use UTF anyway, but it is not null safe. I may end up trying to use the utf package source code, but I am checking here to see if there is a native (or pub) implementation I have missed, as I am new to the world of UTF and byte conversions.
My goal:
encodeAsUtf16le(String s);
I do not need to write a BOM.
Dart
Strings internally use UTF-16. You can useString.codeUnitsto get the UTF-16 code units and then write them in little-endian form:or assume that you're running on a little-endian system:
Also see https://stackoverflow.com/a/67802971/, which is about encoding UTF-16LE to
Strings.I also feel compelled to advise against writing UTF-16 to disk unless you're forced to by external requirements.