public static byte[] GBKToUn(byte[] buffer, int index, int length) { List <byte> data = new List <byte>(); int i = index; length += index; while (i < length) { if (buffer[i] < 128) { data.Add(buffer[i]); data.Add(0x00); i++; } else { int value = buffer[i]; value = ((value << 8) & 0xff00) | (buffer[i + 1] & 0xff); value = GBKTools.DichotomySearch(GBKToUnicodeTable.ucs_MappingTable, (UInt16)0, (UInt16)(GBKToUnicodeTable.ucs_MappingTable.Length / 2), (UInt16)value); int temp = (value >> 8) & 0xff; value = value & 0x00ff; data.Add((byte)value); data.Add((byte)temp); i += 2; } } return(data.ToArray()); }
public static byte[] UnToGBK(byte[] buffer, int index, int length) { List <byte> data = new List <byte>(); int i = index; length += index; while (i < length) { if (buffer[i] < 128 && buffer[i + 1] == 0) { data.Add(buffer[i]); } else { int value = buffer[i] | ((buffer[i + 1] << 8) & 0xff00); byte[] bt = System.BitConverter.GetBytes(GBKTools.DichotomySearch(UnicodeToGBKTable.ucs_MappingTable, (UInt16)0, (UInt16)(UnicodeToGBKTable.ucs_MappingTable.Length / 2), (UInt16)value)); data.Add(bt[1]); data.Add(bt[0]); } ++i; ++i; } return(data.ToArray()); }