Last active
October 25, 2019 11:01
-
-
Save Arathi/786ff6a87979274f11009700cd3bf0e9 to your computer and use it in GitHub Desktop.
生成Shift-JIS码表
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Text; | |
namespace ShiftJisCodeTable | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Encoding ShiftJIS = Encoding.GetEncoding("Shift-JIS", new EncoderExceptionFallback(), new DecoderExceptionFallback()); | |
Encoding GB2312 = Encoding.GetEncoding("GB2312", new EncoderExceptionFallback(), new DecoderExceptionFallback()); | |
Encoding GBK = Encoding.GetEncoding("GBK", new EncoderExceptionFallback(), new DecoderExceptionFallback()); | |
IList<int> codes = new List<int>(); | |
for (int b = 0x20; b <= 0x7E; b++) codes.Add(b); | |
for (int b = 0xA1; b <= 0xDF; b++) codes.Add(b); | |
for (int highByte = 0x81; highByte <= 0x9F; highByte++) | |
{ | |
for (int lowByte = 0x40; lowByte <= 0xFC; lowByte++) | |
{ | |
if (lowByte == 0x7F) continue; | |
codes.Add((highByte << 8) | lowByte); | |
} | |
} | |
for (int highByte = 0xE0; highByte <= 0xEF; highByte++) | |
{ | |
for (int lowByte = 0x40; lowByte <= 0xFC; lowByte++) | |
{ | |
if (lowByte == 0x7F) continue; | |
codes.Add((highByte << 8) | lowByte); | |
} | |
} | |
for (int highByte = 0xFA; highByte <= 0xFC; highByte++) | |
{ | |
for (int lowByte = 0x40; lowByte <= 0xFC; lowByte++) | |
{ | |
if (lowByte == 0x7F) continue; | |
codes.Add((highByte << 8) | lowByte); | |
} | |
} | |
StringBuilder sb = new StringBuilder(); | |
Dictionary<int, int> unicode2shiftjis = new Dictionary<int, int>(); | |
foreach (int code in codes) | |
{ | |
byte[] data = null; | |
try | |
{ | |
if (code > 0xFF) | |
{ | |
data = new byte[2]; | |
data[1] = (byte)(code & 0xFF); | |
data[0] = (byte)((code >> 8) & 0xFF); | |
} | |
else | |
{ | |
data = new byte[1]; | |
data[0] = (byte)(code & 0xFF); | |
} | |
string str = ShiftJIS.GetString(data); | |
if (str.Length != 1) | |
{ | |
continue; | |
} | |
char ch = str[0]; | |
string gbkCodeStr = "-"; | |
try | |
{ | |
byte[] gbkBytes = GB2312.GetBytes(str); | |
int gbkCode = 0; | |
if (gbkBytes.Length == 1) | |
{ | |
gbkCode = gbkBytes[0]; | |
} | |
else if (gbkBytes.Length == 2) | |
{ | |
gbkCode = gbkBytes[0] << 8 | gbkBytes[1]; | |
} | |
gbkCodeStr = string.Format("{0:X4}", gbkCode); | |
} | |
catch (Exception ex) | |
{ | |
// | |
} | |
string pair = string.Format("{0:X4}={1}(&#{2})(GBK:{3})", code, ch, (int)ch, gbkCodeStr); // (&#{2}) | |
if (unicode2shiftjis.ContainsKey((int)ch)) unicode2shiftjis[(int)ch]++; | |
else unicode2shiftjis[(int)ch] = 1; | |
Console.WriteLine(pair); | |
sb.Append(pair); | |
sb.Append("\r\n"); | |
} | |
catch (ArgumentNullException ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
catch (DecoderFallbackException ex) | |
{ | |
// Console.WriteLine(ex.Message); | |
} | |
catch (ArgumentException ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
} | |
File.WriteAllText(@"D:\temp\shift-jis.tbl", sb.ToString()); | |
foreach (var entry in unicode2shiftjis) | |
{ | |
if (entry.Value > 1) | |
Console.WriteLine(string.Format("{0:X4}\t{1}", entry.Key, entry.Value)); | |
} | |
Console.WriteLine("按任意键结束"); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment