unicode2gbkOne function

int unicode2gbkOne(
  1. int unicode
)

Converts a single Unicode character to its corresponding GBK encoding. Returns 0 if the Unicode character is not in the GBK range.

Implementation

int unicode2gbkOne(int unicode) {
  // Handle ASCII characters directly
  if (unicode < 0x80) {
    return unicode;
  }

  var offset = 0;

  if (unicode >= 0x4e00 && unicode <= 0x9fa5) {
    // CJK Unified Ideographs range
    offset = unicode - 0x4e00;
  } else if (unicode >= 0xff01 && unicode <= 0xff61) {
    // Fullwidth Forms range
    offset = unicode - 0xff01 + 0x9fa6 - 0x4e00;
  } else {
    // Character not in GBK range
    return 0;
  }

  // Check bounds before accessing the array
  if (offset < 0 || offset >= gbkTables.length) {
    return 0;
  }

  return gbkTables[offset];
}