Created
May 31, 2018 13:41
-
-
Save kekekeks/e0309d9494e250320c450c53fd7309e3 to your computer and use it in GitHub Desktop.
OEM encoding C#
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
using System.Linq; | |
using System.Text; | |
class OemEncoding : Encoding | |
{ | |
private static readonly char[] Oem = | |
new[] | |
{ | |
127, 199, 252, 233, 226, 228, 224, 229, 231, 234, 235, 232, 239, 238, 236, 196, 197, 201, 230, 198, 244, | |
246, 242, 251, 249, 255, 214, 220, 162, 163, 165, 8359, 402, 225, 237, 243, 250, 241, 209, 170, 186, 191, | |
8976, 172, 189, 188, 161, 171, 187, 9617, 9618, 9619, 9474, 9508, 9569, 9570, 9558, 9557, 9571, 9553, | |
9559, 9565, 9564, 9563, 9488, 9492, 9524, 9516, 9500, 9472, 9532, 9566, 9567, 9562, 9556, 9577, 9574, | |
9568, 9552, 9580, 9575, 9576, 9572, 9573, 9561, 9560, 9554, 9555, 9579, 9578, 9496, 9484, 9608, 9604, | |
9612, 9616, 9600, 945, 223, 915, 960, 931, 963, 181, 964, 934, 920, 937, 948, 8734, 966, 949, 8745, 8801, | |
177, 8805, 8804, 8992, 8993, 247, 8776, 176, 8729, 183, 8730, 8319, 178, 9632, 160 | |
}.Select(x => ((char) x)).ToArray(); | |
private static readonly char[] OemToChar = new char[byte.MaxValue + 1]; | |
private static readonly byte[] CharToOem = new byte[char.MaxValue + 1]; | |
static OemEncoding() | |
{ | |
for (var i = 0; i <= byte.MaxValue; i++) | |
{ | |
var b = (byte) i; | |
if (b < 0x20) | |
OemToChar[b] = ' '; | |
else if (b >= 127) | |
OemToChar[b] = Oem[b - 127]; | |
else | |
OemToChar[b] = (char) b; | |
} | |
for (var i = 0; i <= char.MaxValue; i++) | |
{ | |
var c = (char) i; | |
if (c < 127) | |
CharToOem[c] = (byte) c; | |
else | |
CharToOem[c] = (byte)'?'; | |
} | |
for (var i = 0; i < Oem.Length; i++) | |
CharToOem[Oem[i]] = (byte) i; | |
} | |
public override string BodyName => "OEM"; | |
public override string EncodingName => "OEM"; | |
public override int CodePage => 437; | |
public override int GetByteCount(char[] chars, int index, int count) | |
{ | |
return count; | |
} | |
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) | |
{ | |
for (var c = 0; c < charCount; c++) | |
bytes[byteIndex + c] = CharToOem[chars[charIndex + c]]; | |
return charCount; | |
} | |
public override int GetCharCount(byte[] bytes, int index, int count) | |
{ | |
return count; | |
} | |
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) | |
{ | |
for (var c = 0; c < byteCount; c++) | |
chars[charIndex + c] = OemToChar[bytes[byteIndex + c]]; | |
return byteCount; | |
} | |
public override int GetMaxByteCount(int charCount) | |
{ | |
return charCount; | |
} | |
public override int GetMaxCharCount(int byteCount) | |
{ | |
return byteCount; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment