unicode2gbk function

List<int> unicode2gbk(
  1. List<int> unicodeBytes
)

Converts a list of Unicode characters to their corresponding GBK encoding. If a character is not in the GBK range, it is skipped.

Implementation

List<int> unicode2gbk(List<int> unicodeBytes) {
  List<int> resp = [];

  // Fix the loop - use unicodeBytes.length instead of resp.length
  for (int i = 0; i < unicodeBytes.length; i++) {
    var unicode = unicodeBytes[i];

    if (unicode <= 0x7F) {
      // ASCII characters
      resp.add(unicode);
    } else {
      var value = unicode2gbkOne(unicode);

      if (value == 0) continue;

      // Add high byte and low byte
      resp.add((value >> 8) & 0xff);
      resp.add(value & 0xff);
    }
  }

  return resp;
}