본문 바로가기

프로그래밍

[팁] 16진수 유니코드값을 한글로 전환하는 사례

http://people.w3.org/rishida/scripts/uniview/conversion.php
http://people.w3.org/rishida/scripts/uniview/uniview.php?codepoints=D22C

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.IO;
namespace 한글디코딩
{
	/// 
	/// Form1에 대한 요약 설명입니다.
	/// 
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox tbInput;
		private System.Windows.Forms.TextBox tbResult;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Button btStart;
		private const string FILE_NAME = "TempFile.txt";
		private System.Windows.Forms.TextBox tbResult2;
		private System.Windows.Forms.CheckBox cbSelect;
		

		/// 
		/// 필수 디자이너 변수입니다.
		/// 
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows Form 디자이너 지원에 필요합니다.
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent를 호출한 다음 생성자 코드를 추가합니다.
			//
		}

		/// 
		/// 사용 중인 모든 리소스를 정리합니다.
		/// 
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form 디자이너에서 생성한 코드
		/// 
		/// 디자이너 지원에 필요한 메서드입니다.
		/// 이 메서드의 내용을 코드 편집기로 수정하지 마십시오.
		/// 
		private void InitializeComponent()
		{
			this.tbInput = new System.Windows.Forms.TextBox();
			this.tbResult = new System.Windows.Forms.TextBox();
			this.panel1 = new System.Windows.Forms.Panel();
			this.btStart = new System.Windows.Forms.Button();
			this.tbResult2 = new System.Windows.Forms.TextBox();
			this.cbSelect = new System.Windows.Forms.CheckBox();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// tbInput
			// 
			this.tbInput.Location = new System.Drawing.Point(8, 16);
			this.tbInput.Name = "tbInput";
			this.tbInput.Size = new System.Drawing.Size(560, 21);
			this.tbInput.TabIndex = 0;
			this.tbInput.Text = "";
			// 
			// tbResult
			// 
			this.tbResult.Location = new System.Drawing.Point(0, 56);
			this.tbResult.Multiline = true;
			this.tbResult.Name = "tbResult";
			this.tbResult.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.tbResult.Size = new System.Drawing.Size(792, 250);
			this.tbResult.TabIndex = 1;
			this.tbResult.Text = "";
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.cbSelect);
			this.panel1.Controls.Add(this.btStart);
			this.panel1.Controls.Add(this.tbInput);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
			this.panel1.Location = new System.Drawing.Point(0, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(792, 56);
			this.panel1.TabIndex = 2;
			// 
			// btStart
			// 
			this.btStart.Location = new System.Drawing.Point(696, 16);
			this.btStart.Name = "btStart";
			this.btStart.TabIndex = 1;
			this.btStart.Text = "시작";
			this.btStart.Click += new System.EventHandler(this.btStart_Click);
			// 
			// tbResult2
			// 
			this.tbResult2.Location = new System.Drawing.Point(0, 328);
			this.tbResult2.Multiline = true;
			this.tbResult2.Name = "tbResult2";
			this.tbResult2.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.tbResult2.Size = new System.Drawing.Size(792, 250);
			this.tbResult2.TabIndex = 3;
			this.tbResult2.Text = "";
			// 
			// cbSelect
			// 
			this.cbSelect.Location = new System.Drawing.Point(584, 16);
			this.cbSelect.Name = "cbSelect";
			this.cbSelect.TabIndex = 2;
			this.cbSelect.Text = "간단히";
			this.cbSelect.CheckedChanged += new System.EventHandler(this.cbSelect_CheckedChanged);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(792, 566);
			this.Controls.Add(this.tbResult2);
			this.Controls.Add(this.tbResult);
			this.Controls.Add(this.panel1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "한글디코딩";
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// 
		/// 해당 응용 프로그램의 주 진입점입니다.
		/// 
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void btStart_Click(object sender, System.EventArgs e)
		{
			if(cbSelect.Checked)
			{
				txtWrite(tbInput.Text);
				txtRead();
			}
			else 
			{
				txtWrite(tbResult.Text);
				txtRead();
			} 

		}
                     /// 
                     /// \uc1c2 형태의 유니코드를 한글로 변환시켜는 함수
                     /// 
                     /// 
                     /// 
		private string HanConvert(string str)
		{
			string text1 = str;
			string temp = "";
			int index=0;
			char ch;
            
			if(text1 == String.Empty || text1.Equals(""))
			{
				return "";
			}

			while(text1.Length > 4 && text1.IndexOf(@"\u") > -1)
			{
				index = text1.IndexOf(@"\u");
				if(index > 0)
				{
					temp += text1.Substring(0,index);
					text1= text1.Remove(0,index);
				}
				if(text1.Length > index)
				{
					ch = (char)(Convert.ToInt16(text1.Substring(2,4), 16));
					temp += ch.ToString();	
					text1 =text1.Remove(0,6);
				}
			}
			temp += text1;
                                return temp;

		}

		/// 
		/// 임시로 파일에 저장하는 메소드
		/// 
		/// 

		private void txtWrite(string str)
		{
			try
			{
				if (File.Exists(FILE_NAME)) 
				{
					MessageBox.Show(FILE_NAME+"파일이 존재합니다");
					return;
				}
				FileStream fs = new  FileStream(FILE_NAME,FileMode.Create); 
				StreamWriter sw = new StreamWriter(fs); 
				sw .Write Line(str);
				sw.Close();
				
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		/// 
		/// 임시파일에 저장된 내용을 읽어오는 메소드
		/// 

		private void txtRead()
		{
			try 
			{
				using (StreamReader sr = new StreamReader(FILE_NAME)) 
				{
					String line;
					StringBuilder sb = new StringBuilder();
					
					while ((line = sr.ReadLine()) != null) 
					{
						sb.Append(HanConvert(line)+"\r\n");
					}
					tbResult2.Text=sb.ToString();
					
				}
				//읽은 파일 삭제
				File.Delete(FILE_NAME);
				
			}
			catch (Exception e) 
			{
				MessageBox.Show(e.Message);
				
			}


		}

		private void cbSelect_CheckedChanged(object sender, System.EventArgs e)
		{
			if(cbSelect.Checked)
			{
				tbInput.Focus();
				tbInput.Text = "";
			}
		
		}
	}
}