encodeUtf16be function

List<int?> encodeUtf16be(
  1. String str, [
  2. bool writeBOM = false
])

Produce a list of UTF-16BE encoded bytes. By default, this method produces UTF-16BE bytes with no BOM.

Implementation

List<int?> encodeUtf16be(String str, [bool writeBOM = false]) {
  var utf16CodeUnits = _stringToUtf16CodeUnits(str);
  var encoding = <int?>[]..length =
      2 * utf16CodeUnits.length + (writeBOM ? 2 : 0);
  var i = 0;
  if (writeBOM) {
    encoding[i++] = UNICODE_UTF_BOM_HI;
    encoding[i++] = UNICODE_UTF_BOM_LO;
  }
  for (var unit in utf16CodeUnits) {
    encoding[i++] = (unit! & UNICODE_BYTE_ONE_MASK) >> 8;
    encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK;
  }
  return encoding;
}