convert method

  1. @override
Uint8List convert(
  1. String string, [
  2. int start = 0,
  3. int? end
])
override

Dart does not support non-unicode encoding. Thus, the return value has to be raw byte array of CP949.

Implementation

@override
Uint8List convert(String string, [int start = 0, int? end]) {
  int stringLength = string.codeUnits.length;
  end = RangeError.checkValidRange(start, end, stringLength);

  if (start == end) return Uint8List(0);

  // 2byte
  BytesBuilder cp949CodeUnits = BytesBuilder();

  // int i = 0;

  for (int unicode in string.codeUnits) {
    final cp949Code = unicodeToCp949CodeMap[unicode];

    if (cp949Code == null) {
      throw FormatException("the code map for $unicode is null");
    }

    final firstByte = cp949Code >> 8;
    final secondByte = cp949Code - (firstByte << 8);

    // 2 byte
    if (firstByte != 0) {
      cp949CodeUnits.addByte(firstByte);
    }
    cp949CodeUnits.addByte(secondByte);
    // cp949codeUnits[i] = secondByte;
    // i = i + 1;
  }

  return cp949CodeUnits.toBytes();
}