Skip to content

Instantly share code, notes, and snippets.

@whc2001
Created August 9, 2016 13:37
Show Gist options
  • Save whc2001/c80118b245e41c0ecdb3794fc05eca0c to your computer and use it in GitHub Desktop.
Save whc2001/c80118b245e41c0ecdb3794fc05eca0c to your computer and use it in GitHub Desktop.
C# Convert Hiragana To Katakana By Unicode
public string HiraganaToKatakana(string input)
{
byte[] unicodes = Encoding.GetEncoding("Unicode").GetBytes(input);
int i;
for (i = 0; i < unicodes.Length; i += 2) //Each 16 bits.
{
int _word = (unicodes[i + 1] << 8) | (unicodes[i] & 0xFF); //Two byte make a word.
if (_word >= 0x3041 && _word <= 0x30A0) //In hiragana area?
{
_word += 0x60; //Add difference.
unicodes[i + 1] = (byte)(_word >> 8); //Write back high byte.
unicodes[i] = (byte)(_word & 0xFF); //Write back low byte.
}
}
return Encoding.GetEncoding("Unicode").GetString(unicodes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment