Search code examples
dartutf-16le

How to encode to UTF16 Little Endian in Dart?


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.


Solution

  • Dart Strings internally use UTF-16. You can use String.codeUnits to get the UTF-16 code units and then write them in little-endian form:

      var s = '\u{1F4A9}';
      var codeUnits = s.codeUnits;
      var byteData = ByteData(codeUnits.length * 2);
      for (var i = 0; i < codeUnits.length; i += 1) {
        byteData.setUint16(i * 2, codeUnits[i], Endian.little);
      }
      
      var bytes = byteData.buffer.asUint8List();
      await File('output').writeAsBytes(bytes);
    

    or assume that you're running on a little-endian system:

      var s = '\u{1F4A9}';
      var codeUnits = s.codeUnits;
      var bytes = Uint16List.fromList(codeUnits).buffer.asUint8List();
      await File('output').writeAsBytes(bytes);
    

    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.