Created
August 9, 2016 13:37
-
-
Save whc2001/c80118b245e41c0ecdb3794fc05eca0c to your computer and use it in GitHub Desktop.
C# Convert Hiragana To Katakana By Unicode
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
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