반응형

*C# String 을 Byte 로 Byte 를 String 으로 String <-> Char 변환 예제...

 

Main

- 사용한 컨트롤: Button 3개, TextBox 1개

전체 소스 코드

Form1.cs

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        String a = "ㄱㄴㄷ123abc";
        byte[] bb = null;
        byte[] bc = null;
        byte[] bd = null;
        byte[] be = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //String -> Byte 

            bb= System.Text.Encoding.Default.GetBytes(a); // String to default byte
            bc= System.Text.Encoding.Unicode.GetBytes(a); // String to unicode byte
            bd= System.Text.Encoding.UTF8.GetBytes(a);  // String to UTF8 byte
            be= System.Text.Encoding.ASCII.GetBytes(a); // 하위 7bit 만 변환됨.

            textBox1.Text = "String -> Byte 로 변환 ============== " + System.Environment.NewLine ; 
            textBox1.Text += "길이 " + System.Environment.NewLine;
            textBox1.Text += "Default: " + bb.Length + System.Environment.NewLine;
            textBox1.Text += "Unicode: " + bc.Length + System.Environment.NewLine;
            textBox1.Text += "UTF8: " + bd.Length + System.Environment.NewLine;
            textBox1.Text += "ASCII: " + be.Length + System.Environment.NewLine;
            textBox1.Text += "===================================== " + System.Environment.NewLine + System.Environment.NewLine;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Byte -> String

            //바이트 배열에 아무것도 없으면...
            if ((bb == null) || (bc == null) || (bd == null) || (be == null))
            {
                MessageBox.Show("String -> Byte 변환 버튼 클릭 후 이용해 주세요.", "확 인", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            
            textBox1.Text += "Byte -> String 로 변환 ============== " + System.Environment.NewLine;
            textBox1.Text += "Default: " + System.Text.Encoding.Default.GetString(bb) + System.Environment.NewLine;
            textBox1.Text += "Unicode: " + System.Text.Encoding.Unicode .GetString(bc) + System.Environment.NewLine;
            textBox1.Text += "UTF8: " + System.Text.Encoding.UTF8 .GetString(bd) + System.Environment.NewLine;
            textBox1.Text += "ASCII: " + System.Text.Encoding.ASCII .GetString(be) + System.Environment.NewLine;            //<- 한글 깨짐 한글 유니코드 변환X
            textBox1.Text += "===================================== " + System.Environment.NewLine + System.Environment.NewLine;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // String <-> Char
            string sTmp = "가나다123abc";
            char[] cTmp = sTmp.ToCharArray();

            textBox1.Text += "String -> Char 로 변환 ============== " + System.Environment.NewLine;
            textBox1.Text += "Char 길이:  -> " + cTmp.Length  + System.Environment.NewLine;
            textBox1.Text += "Char -> String 로 변환 ============== " + System.Environment.NewLine;
            textBox1.Text += "변환: " + new string(cTmp) + System.Environment.NewLine;
            textBox1.Text += "===================================== " + System.Environment.NewLine + System.Environment.NewLine;
        }
    }
}

 

 

* 예제 결과

 

 

반응형

+ Recent posts