Last active
December 19, 2015 17:28
-
-
Save Aimeast/5991026 to your computer and use it in GitHub Desktop.
Encoding bin files generator
This file contains hidden or 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
// Encoding bin files generator for Encoding for Silverlight project | |
// https://encoding4silverlight.codeplex.com/ | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
class Program | |
{ | |
private const char LEAD_BYTE_CHAR = '\uFFFE'; | |
private const char UNICODE_REPLACEMENT_CHAR = '\uFFFD'; | |
static void Main(string[] args) | |
{ | |
var dbcs = Encoding.GetEncoding(936).GetType(); // Chinese Simplified (GB2312) | |
var encodings = Encoding.GetEncodings().Where(s => s.GetEncoding().GetType() == dbcs).ToArray(); | |
foreach (var ei in encodings) | |
{ | |
Encoding e = ei.GetEncoding(); | |
bool[] flags = new bool[0x10000]; | |
using (FileStream fs = new FileStream(ei.Name + ".bin", FileMode.Create)) | |
using (BinaryWriter writer = new BinaryWriter(fs)) | |
{ | |
for (int i = 0; i < 0xffff; i++) | |
{ | |
string s1 = ((char)i).ToString(); | |
byte[] buf = e.GetBytes(s1); | |
ushort u = 0; | |
if (buf.Length == 1) | |
{ | |
u = buf[0]; | |
} | |
else | |
{ | |
u = (ushort)(buf[0] << 8 | buf[1]); | |
flags[buf[0]] = true; | |
} | |
if (i == UNICODE_REPLACEMENT_CHAR) | |
u = 0; | |
writer.Write(u); | |
} | |
for (int i = 0; i < 0xffff; i++) | |
{ | |
byte[] buf = new byte[2]; | |
if (i < 0x100) | |
{ | |
buf[0] = (byte)(i & 0xff); | |
} | |
else | |
{ | |
buf[0] = (byte)((i >> 8) & 0xff); | |
buf[1] = (byte)(i & 0xff); | |
} | |
ushort u = (ushort)e.GetChars(buf)[0]; | |
if (flags[i]) | |
u = LEAD_BYTE_CHAR; | |
writer.Write(u); | |
} | |
fs.Flush(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment