■ 한자를 한글로 변환하는 방법을 보여준다.
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
using System; using System.Globalization; using System.IO; using System.Text; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Console.Title = "한자를 한글로 변환하기"; int[] conversionMapArray = GetConversionMapArray("code.dat"); string result = GetHangulString(conversionMapArray, "南山 타워"); Console.WriteLine(result); } #endregion #region 변환 맵 배열 구하기 - GetConversionMapArray(filePath) /// <summary> /// 변환 맵 배열 구하기 /// </summary> /// <param name="filePath">파일 경로</param> /// <returns>변환 맵 배열</returns> private static int[] GetConversionMapArray(string filePath) { int[] resultArray = new int[65536]; string sourceData = File.ReadAllText(filePath).Trim().Replace("\r\n", "\n"); string[] lineArray = sourceData.Split('\n'); int i = 0; foreach(string line in lineArray) { if(line.StartsWith("/*")) { continue; } string[] itemArray = line.Split(','); foreach(string item in itemArray) { if(string.IsNullOrEmpty(item)) { continue; } int code = int.Parse(item.Substring(2), NumberStyles.HexNumber); resultArray[i] = code; i++; } } return resultArray; } #endregion #region 한글 문자열 구하기 - GetHangulString(conversionMapArray, source) /// <summary> /// 한글 문자열 구하기 /// </summary> /// <param name="conversionMapArray">변환 맵 배열</param> /// <param name="source">소스 문자열</param> /// <returns>한글 문자열</returns> private static string GetHangulString(int[] conversionMapArray, string source) { int code = 0; byte[] sourceArray = Encoding.UTF8.GetBytes(source); int sourceArrayLength = sourceArray.Length; for(int i = 0; i < sourceArrayLength;) { if((sourceArray[i] & 0xff) < 0x80) { i++; continue; } else if((sourceArray[i] & 0xff) < 0xe0) { i += 2; continue; } else if((sourceArray[i] & 0xff) < 0xf0) { code = sourceArray[i] & 0x0f; i++; code = code << 6; code = code | (sourceArray[i] & 0x3f); i++; code = code << 6; code = code | (sourceArray[i] & 0x3f); i++; } if(conversionMapArray[code] != code) { code = conversionMapArray[code]; sourceArray[i - 1] = (byte)((code & 0x3f) | 0x80); sourceArray[i - 2] = (byte)(((code << 2) & 0x3f00 | 0x8000) >> 8); sourceArray[i - 3] = (byte)(((code << 4) & 0x3f0000 | 0xe00000) >> 16); continue; } } string target = Encoding.UTF8.GetString(sourceArray); return target; } #endregion } } |