■ 한자를 한글로 변환하기

----------------------------------------------------------------------------------------------------


TestProject.zip


Program.cs

 

 

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()

        {

            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

    }

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요