[VB] Instructions for converting between binary (Bin), decimal (Dec) and hexadecimal (Hex) numbers

Hi mọi người, bài viết hôm nay mình sẽ hướng dẫn các bạn cách chuyển đổi qua lại giữa các hệ cơ số: nhị phân (Binary), thập phân (Decimal), thập lục phân (Hexadecimal) trong lập trình VB.NET.
[VB] Instructions for converting between binary (Bin), decimal (Dec) and hexadecimal (Hex) numbers

1. Hệ nhị phân là gì?

Hệ nhị phân (hay hệ đếm cơ số hai) là một hệ đếm dùng hai ký tự để biểu đạt một giá trị số, bằng tổng số các lũy thừa của 2. Như vậy, hệ nhị phân (tiếng Anh gọi là binary) là hệ đếm chỉ dùng 2 chữ số thay vì 10 chữ số như hệ thập phân, và 2 chữ số này thường là 0 và 1. Chính cái tên của nó cũng nói lên điều này: “nhị” tức là 2, “thập” tức là 10.

2. Hệ thập phân là gì?

Hệ thập phân (hệ đếm cơ số 10) là hệ đếm dùng số 10 làm cơ số. Đây là hệ đếm được sử dụng rộng rãi nhất trong các nền văn minh thời hiện đại.

3. Hệ thập lục phân là gì?

Hệ thập lục phân (hay hệ đếm cơ số 16, tiếng Anh: hexadecimal), hoặc chỉ đơn thuần gọi là thập lục, là một hệ đếm có 16 ký tự, từ 0 đến 9 và A đến F (chữ hoa và chữ thường như nhau). Hệ thống thập lục phân hiện dùng, được công ty IBM giới thiệu với thế giới điện toán vào năm 1963. Một phiên bản cũ của hệ thống này, dùng các con số từ 0 đến 9, và các con chữ A đến F, đã được sử dụng trong máy tính Bendix G-15, ra mắt năm 1956.

Source code convert bin, dec, hex VB.NET:

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Strconvert As String = TextBox1.Text
        Dim Decimals As Integer
        Dim hexadecimal As String
        Dim Binary As String
        Dim x As Integer

        ' convert Binary to Binary
        If ComboBox1.Text = "Binary" And ComboBox2.Text = "Binary" Then
            TextBox2.Text = TextBox1.Text
            ' convert Binary to Decimal
        ElseIf ComboBox1.Text = "Binary" And ComboBox2.Text = "Decimal"
            Decimals = Convert.ToInt32(Strconvert, 2)
            TextBox2.Text = Decimals.ToString
            ' convert Binary to Hexadecimal
        ElseIf ComboBox1.Text = "Binary" And ComboBox2.Text = "Hexadecimal"
            Decimals = Convert.ToInt32(Strconvert, 2)
            hexadecimal = Convert.ToString(Decimals, 16)
            TextBox2.Text = hexadecimal
            ' convert Decimal to Binary
        ElseIf ComboBox1.Text = "Decimal" And ComboBox2.Text = "Binary"
            x = Convert.ToInt32(Strconvert)
            Binary = Convert.ToString(x, 2)
            TextBox2.Text = Binary
            ' convert Decimal to Hexadecimal
        ElseIf ComboBox1.Text = "Decimal" And ComboBox2.Text = "Hexadecimal"
            x = Convert.ToInt32(Strconvert)
            hexadecimal = Convert.ToString(x, 16)
            TextBox2.Text = hexadecimal
            ' convert Hexadecimal to Binary
        ElseIf ComboBox1.Text = "Hexadecimal" And ComboBox2.Text = "Binary"
            Decimals = Convert.ToInt32(Strconvert, 16)
            Binary = Convert.ToString(Decimals, 2)
            TextBox2.Text = Binary
            ' convert Hexadecimal to Decimal
        ElseIf ComboBox1.Text = "Hexadecimal" And ComboBox2.Text = "Decimal"
            Decimals = Convert.ToInt32(Strconvert, 16)
            TextBox2.Text = Decimals.ToString
        Else
            If ComboBox1.Text = "Select Format" Or ComboBox2.Text = "Select Format" Or ComboBox1.Text = "" Or ComboBox2.Text = "" Then
                MsgBox("Please Insert Select type to convert")
            End If
        End If
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Add items into ComboBox1
        ComboBox1.Items.Add("Binary")
        ComboBox1.Items.Add("Decimal")
        ComboBox1.Items.Add("Hexadecimal")
        ' Add items into ComboBox1
        ComboBox2.Items.Add("Binary")
        ComboBox2.Items.Add("Decimal")
        ComboBox2.Items.Add("Hexadecimal")
        ' Add Text into ComboBox1
        ComboBox1.Text = "Select Format"
        ' Add Text into ComboBox1
        ComboBox2.Text = "Select Format"
    End Sub
End Class

Chúc các bạn thành công với thủ thuật trên và tuỳ biến để sữ dụng cho mục đích cá nhân thật tốt nhé.
Theo LapTrinhVB

PASSWORD UNZIP: HUNG.PRO.VN
Chúc Mọi Người Thành Công Với Thủ Thuật Trên.
Nếu mọi người có vướng mắc gì mình chia sẽ trên trang có thể gửi liên hê cho mình tại đây nhé.
Cảm ơn mọi người đã quan tâm.

No comments:

All Right Reserved © 2015 By Hung Pro VN