Skip to content

Instantly share code, notes, and snippets.

@biac
Created December 8, 2012 08:54
Show Gist options
  • Save biac/4239360 to your computer and use it in GitHub Desktop.
Save biac/4239360 to your computer and use it in GitHub Desktop.
Win8 Metro で MultiByteToWideChar を使う
public class TextDecoder
{
[DllImport("Kernel32.dll", CharSet = CharSet.Unicode)]
static extern int MultiByteToWideChar(
uint CodePage,
uint dwFlags,
[MarshalAs(UnmanagedType.LPArray)] byte[] lpMultiByteStr,
int cbMultiByte,
[Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpWideCharStr,
int cchWideChar);
public static string ReadAsShiftJis(byte[] bArray)
{
const uint CP_OEMCP = 1U;
StringBuilder translatedString = new StringBuilder(bArray.Length + 1);
MultiByteToWideChar(CP_OEMCP, 0, bArray, -1,
translatedString, translatedString.Capacity);
// WP8 では、実行時に↑ココで例外。
return translatedString.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment