반응형

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

 

Main

 

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

전체 소스 코드

Form1.vb

Public Class Form1

    Dim a As String = "ㄱㄴㄷ123abc"
    Dim bb As Byte() = Nothing
    Dim bc As Byte() = Nothing
    Dim bd As Byte() = Nothing
    Dim be As Byte() = Nothing

    Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
        'String => Byte
        bb = System.Text.Encoding.Default.GetBytes(a)
        bc = System.Text.Encoding.Unicode.GetBytes(a)
        bd = System.Text.Encoding.UTF8.GetBytes(a)
        be = System.Text.Encoding.ASCII.GetBytes(a)

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

    End Sub

    Private Sub button2_Click(sender As Object, e As EventArgs) Handles button2.Click
        'Byte => String
        '바이트 배열에 아무것도 없으면...
        If (bb Is Nothing) And (bc Is Nothing) And (bd Is Nothing) And (be Is Nothing) Then
            MessageBox.Show("String -> Byte 변환 버튼 클릭 후 이용해 주세요.", "확 인", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            Return
        End If

        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

    End Sub

    Private Sub button3_Click(sender As Object, e As EventArgs) Handles button3.Click
        'String <=> Char
        Dim sTmp As String = "가나다123abc"
        Dim cTmp As Char() = sTmp.ToCharArray()

        textBox1.Text += "String -> Char 로 변환 ============== " + System.Environment.NewLine
        textBox1.Text += "Char 길이:  -> " + cTmp.Length.ToString() + 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

    End Sub
End Class

 

* 예제 결과

반응형

+ Recent posts