퇴근5분전

이번 자바 작업중에 유니코드를 다루는게 있어서...

VS Express 설치해서 변환툴을 만들때 사용한 코드임!

 


## 한글 -> 유니코드
            string txt = UniCodeText.Text;
            string resultUniCode = "";
           
            foreach (char s in txt.ToCharArray())
            {
                if (char.IsLetter("" + s, 0))
                {
                    resultUniCode += "\\u";
                    byte[] bts = UnicodeEncoding.Unicode.GetBytes("" + s);
                    for (int loop = bts.Length - 1; loop >= 0; loop--)
                    {
                        resultUniCode += bts[loop].ToString("x2");
                    }
                }
                else
                {
                    resultUniCode += s;
                }
            } 
            UniCode.Text = resultUniCode;


## 유니코드 -> 한글!

            string Dir = Application.StartupPath + "\\UnicodeConvert\\";

            try
            {
                //unicode된 파일을 변환!!
                using (OpenFileDialog dlg = new OpenFileDialog())
                {
                    dlg.InitialDirectory = HomeDirectory.Text;
                    dlg.FileName = "";
                    if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK && dlg.FileName != "" &&

                        File.Exists(dlg.FileName))
                    {
                        string readFile = File.ReadAllText(dlg.FileName);

                        string writeFileContent = "";

                        char[] uniarray = readFile.ToCharArray();

                        for (int loop = 0; loop < uniarray.Length; )
                        {
                            char c = uniarray[loop];
                            char u = '\0';
                            if (loop + 1 < uniarray.Length) u = uniarray[loop + 1];
                            if (c == '\\' || u == 'u')
                            {
                                try
                                {
                                    byte unibyte00 = Convert.ToByte("" + uniarray[loop + 4] + uniarray[loop + 5], 16);
                                    byte unibyte01 = Convert.ToByte("" + uniarray[loop + 2] + uniarray[loop + 3], 16);
                                    writeFileContent += UnicodeEncoding.Unicode.GetString(new byte[] { unibyte00, unibyte01 });
                                }
                                catch
                                {
                                }
                                finally
                                {
                                    loop += "\\uaaaa".Length;
                                }
                            }
                            else
                            {
                                writeFileContent += c;
                                loop++;
                            }
                        }

                        if (!Directory.Exists(Dir))
                        {
                            Directory.CreateDirectory(Dir);
                        }

                        File.WriteAllText(Dir + Path.GetFileName(dlg.FileName), writeFileContent, UnicodeEncoding.Unicode);

                        UnicodeFilePath.Text = ( Dir + Path.GetFileName(dlg.FileName) ).Replace(HomeDirectory.Text, "")

                                                                                                                  .Replace("\\", "/");
                        if(MessageBox.Show("메모장에서 보시겠습니까?", "확인", 

                                               MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK )
                            System.Diagnostics.Process.Start("notepad.exe", Dir + Path.GetFileName(dlg.FileName));
                    }
                }
            }
            catch( Exception ex ){
                MessageBox.Show(ex.Message);
            }