Created
September 3, 2015 09:21
-
-
Save neguse11/59bf02688d6aa0cfb034 to your computer and use it in GitHub Desktop.
WIN32 APIを利用したSystem.Text.Encodingの実装
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
// WIN32 APIを利用したSystem.Text.Encodingの実装 | |
// パクり元 http://jonskeet.uk/csharp/ebcdic/ | |
public class Win32Encoding : System.Text.Encoding | |
{ | |
uint codePage; | |
[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)] | |
static extern int WideCharToMultiByte(uint CodePage, uint dwFlags, System.IntPtr lpWideCharStr, int cchWideChar, System.IntPtr lpMultiByteStr, int cbMultiByte, System.IntPtr lpDefaultChar, System.IntPtr lpUsedDefaultChar); | |
[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)] | |
static extern int MultiByteToWideChar(uint CodePage, uint dwFlags, System.IntPtr lpMultiByteStr, int cbMultiByte, System.IntPtr lpWideCharStr, int cchWideChar); | |
public static Win32Encoding GetEncoding(uint CodePage) | |
{ | |
return new Win32Encoding(CodePage); | |
} | |
Win32Encoding(uint CodePage) | |
{ | |
this.codePage = CodePage; | |
} | |
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) | |
{ | |
int result; | |
unsafe | |
{ | |
fixed (char* pChars = chars) | |
{ | |
fixed (byte* pBytes = bytes) | |
{ | |
int maxByteCount = GetMaxByteCount(charCount); | |
var lpChars = (System.IntPtr)(pChars + charIndex); | |
var lpBytes = (System.IntPtr)(pBytes + byteIndex); | |
result = WideCharToMultiByte(codePage, 0, lpChars, charCount, lpBytes, maxByteCount, System.IntPtr.Zero, System.IntPtr.Zero); | |
} | |
} | |
} | |
return result; | |
} | |
public override int GetByteCount(char[] chars, int index, int count) | |
{ | |
int result; | |
unsafe | |
{ | |
fixed (char* pChars = chars) | |
{ | |
var lpChars = (System.IntPtr)(pChars + index); | |
result = WideCharToMultiByte(codePage, 0, lpChars, count, System.IntPtr.Zero, 0, System.IntPtr.Zero, System.IntPtr.Zero); | |
} | |
} | |
return result; | |
} | |
public override int GetMaxByteCount(int charCount) | |
{ | |
return charCount * 2; // !!! | |
} | |
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) | |
{ | |
int result; | |
unsafe | |
{ | |
fixed (byte* pBytes = bytes) | |
{ | |
fixed (char* pChars = chars) | |
{ | |
int maxCharCount = GetMaxCharCount(byteCount); | |
var lpChars = (System.IntPtr)(pChars + charIndex); | |
var lpBytes = (System.IntPtr)(pBytes + byteIndex); | |
result = MultiByteToWideChar(codePage, 0, lpBytes, byteCount, lpChars, maxCharCount); | |
} | |
} | |
} | |
return result; | |
} | |
public override int GetCharCount(byte[] bytes, int index, int count) | |
{ | |
int result; | |
unsafe | |
{ | |
fixed (byte* pBytes = bytes) | |
{ | |
var lpBytes = (System.IntPtr)(pBytes + index); | |
result = MultiByteToWideChar(codePage, 0, lpBytes, count, System.IntPtr.Zero, 0); | |
} | |
} | |
return result; | |
} | |
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