แปลงเลขฐาน 16 เป็นตัวอักษร

แปลงเลขฐาน 16 เป็นตัวอักษร
Category »  VB 6/VB.Net
�� : Webmaster ����� 14/7/2553   ����: 09:29
(��ҹ : 74836) 

แปลงเลขฐาน 16 เป็นตัวอักษร
����� �Ե ��� ˹��·����硷���ش�ͧ���������� �ҡ�բ�Ҵ 8 �Ե ����ҡѺ 1 亵� �ѹ���礧��ͧ�����ͺ�ѹ�����������Ф�Ѻ ... ��觷������֡������ͧ�ǡ��������� �����੾�С�ù�����¹��������ŧ�Ţ�ҹ����ҹ�� ���С����¹�������ҡ��ͧ��С�Ȫ�Դ�ͧ����� �� Integer �բ�Ҵ 2 亵� (���� 16 �Ե) Ẻ Long Integer �բ�Ҵ 4 亵� (���� 32 �Ե) �չ�����������ҧ��ҷ�����¡�ѹ�繺Ե ��亵��� �ѹ�������Ѻ�������ҧ �ѹ������͸Ժ������� �����������ҹ������ ... ����ҡ�������ö�Ӥ������������ͧ�ǡ����� ��з�������͡��Դ ��� ��Ҵ�ͧ���������ҹ�����ҧ������� ... �չ������ҡѹ�֧����ͧ����ŧ�Ţ�ҹ ... ��ҵ���� Basic ��������Ш�ҧ�ѹ���� ... 55555+

    ͸Ժ������ͧ�ͧ��á�Ш���Ţ�ҹ 10
  • ����Ţ�ҹ 10 = ... + (10^3 x Dec) + (10^2 x Dec) + (10^1 x Dec) + (10^0 x Dec)
  • (����ͧ���� ^ ��� ���¡���ѧ��Ф�Ѻ)
  • �ǡ 10 ¡���ѧ��ҵ�ҧ��� ������¡��ҹ��˹ѡ��Ш���ѡ (Weight) ��Ф�Ѻ
  • Dec = ��һ�Ш���ѡ��
  • �� 4327 ���� ...
  • = (10^3 x 4) + (10^2 x 3) + (10^1 x 2) + (10^0 x 7)
  • = (1000 x 4) + (100 x 3) + (10 x 2) + (1 x 7)
  • = 4000 + 300 + 20 + 7 = 4327
    �ѧ��鹡�äԴ�Ţ�ҹ 2 ������ѡ������ǡѹ ������¹�ҡ 10 ���¡���ѧ �������� 2 ¡���ѧ᷹
  • ���й�蹤���Ţ�ҹ�����ҵ�ͧ��� (�ҹ 2) �ӹdz���Ţ�ҹ 10
  • �ٵ������㹡���ŧ�Ţ�ҹ 2 ���Ţ�ҹ 10
  • Decimal ���� �Ţ�ҹ 10 = ... + (2^3 x Bit) + (2^2 x Bit) + (2^1 x Bit) + (2^0 x Bit)
  • �ǡ 2 ¡���ѧ��ҵ�ҧ��� ������¡��ҹ��˹ѡ��Ш���ѡ (Weight) ���� ��ШӺԵ
  • ����� Bit �դ���� 0 ���� 1 (�ó��� 0 ���������� ���� 0 �ٳ���á��� 0)
  • �� ... 1 0 1 1 �Ţ�ҹ 2 ...
  • = (2^3 x 1) + (2^2 x 0) + (2^1 x 1) + (2^0 x 1)
  • = (8 x 1) + (4 x 0) + (2 x 1) + (1 x 1)
  • = 8 + 0 + 2 + 1 = 11 (�ҹ 10)
    ��äԴ�Ţ�ҹ 16 ������ѡ������ǡѹ ��� ����¹�ҡ 10 ���¡���ѧ �������� 16 ¡���ѧ᷹
  • ���й�蹤���Ţ�ҹ�����ҵ�ͧ��� (�ҹ 16) �ӹdz���Ţ�ҹ 10
  • �ٵ������㹡���ŧ�Ţ�ҹ 16 ���Ţ�ҹ 10
  • Decimal ���� �Ţ�ҹ 10 = ... + (16^3 x Hex) + (16^2 x Hex) + (16^1 x Hex) + (16^0 x Hex)
  • ����� Hex ��� ��Ңͧ�Ţ�ҹ 16 �������ѡ ���ͧ�Դ���Ţ�ҹ 10 ��͹ ��
  • 0 - 9 �դ����ҡѺ�ҹ 10 �������� ����ͧ�ŧ����� �� A=10, B=11,C=12,D=13,E=14,F=15
  • �� 2EF ...
  • = (16^2 x 2) + (16^1 x 14) + (16^0 x 15)
  • = (256 x 2) + (16 x 14) + (1 x 15)
  • = 512 + 224 + 15 = 751 (�ҹ 10)
  • ��� 2 E ���� F (�ҹ 16) ���е���բ�Ҵ 4 �Ե��ҹ�鹹�Ф�Ѻ

�ѧࡵ��� ��äӹdz�Ҥ�ҹ��˹ѡ (Weight) ��Ш���ѡ ��������Ţ�ҹ���� ¡���ѧ����� 0 (�ҧ�������ش) ������������Ţ¡���ѧ价ҧ������͢��令����� 1 �������� �����Ҩ�����ش��ѡ�ҧ������� ... (���й�����Ф����ѡ��÷����ҵ�ͧ��������¹�������)

�������Ѵਹ�ҡ�Ţ��� 3 �ҹ������ ��ѡ��÷������� (����������ѹ�������) 㹡���ŧ�Ţ�ҹ������Ţ�ҹ 10 �ѹ���������¹���� ��觷������¹ ��� �Ţ�ҹ�����¡���ѧ �����Ҥ�ҹ��˹ѡ (Weight) ��Ш���ѡ ��Ш�¡����Ţ�ҹ 16 �������ͧ�ҡ��ҵ������ 10 - 15 ����Ţ�ѹ�բ�Ҵ 2 ��ѡ �֧����дǡ��͡�ù�����ҹ �ҧ���Դ�к��Ţ�ҹ �֧��Ӥ�� A - F ��᷹��ҷ����Ҵѧ����� �ѧ��鹡�͹�����ŧ�Ţ�ҹ 16 ����Ţ�ҹ 10 �е�ͧ�ա�õ�Ǩ�ͺ���¡�͹�����ѡ���� �դ�� A - F ����������� ... �ҡ�յ�ͧ�ŧ�������������Ţ�ҹ 10 ����ӹǹ�ͧ�ѹ�ͧ��͹ (�Ԩ�óҨҡ���ҧ��ҧ��ҧ��Сͺ)

����ŧ�Ţ�ҹ 16 ���Ţ�ҹ 10 �����Ըա����ѡ����� 2 Ẻ ���
  • �ŧ�ҡ�ҹ 16 ��繰ҹ 10 �ç����� ��������÷ҧ��ҹ��
  • �ŧ�Ţ�ҹ 16 ���Ţ�ҹ 2 ��͹ ���Ǥ����ŧ�Ţ�ҹ 2 ����Ţ�ҹ 10 �ա��
��觼����ʴ��Ըա�� ��� ᨡ������ٷ�� 2 Ẻ������Ф�Ѻ ... �����ҡ��ҹ�Ҷ֧�ç��������ѧ������� ���й���֡������ͧ�ͧ�Ţ�ҹ 10 ��͹��Ҩдա��ҹ�Ф�Ѻ

HEXADECIMAL
(�Ţ�ҹ 16 ���е�Ǩ��� Binary ��Ҵ 4 �Ե)

DECIMAL
(�����Ţ�ҹ 10)

FOUR DIGIT BINARY
(���˹ѡ��Ш���ѡ�ͧ���кԵ)
�Ե 3 �Ե 2 �Ե 1 �Ե 0

2^3 = 8

2^2 = 4

2^1 = 2

2^0 = 1

0

0

0

0

0

0

1

1

0

0

0

1

2

2

0

0

1

0

3

3

0

0

1

1

4

4

0

1

0

0

5

5

0

1

0

1

6

6

0

1

1

0

7

7

0

1

1

1

8

8

1

0

0

0

9

9

1

0

0

1

A

10

1

0

1

0

B

11

1

0

1

1

C

12

1

1

0

0

D

13

1

1

0

1

E

14

1

1

1

0

F

15

1

1

1

1

แปลงเลขฐาน 16 เป็นตัวอักษร

แปลงเลขฐาน 16 เป็นตัวอักษร

แปลงเลขฐาน 16 เป็นตัวอักษร

�ѧ���� ���� ���������㹡���ŧ�Ţ�ҹ 16 �繰ҹ 10 �µç ���¡���������


' #######################################################
' �ѧ���蹡���ŧ�Ţ�ҹ 16 ���Ţ�ҹ 10 ���������
' �·���ա���Ѻ��Ҫش�Ţ�ҹ 16 ����� (String) ���Ǥ׹��ҡ�Ѻ�Ţ�ҹ 10 ��Ѻ�
' #######################################################
Function HexToDecimal(Hex As String) As Double
    Dim i As Integer
    ' �纤���Ţ�ҹ 10 �繵����Ẻ Double �բ�Ҵ 8 亵�
    ' �ҡ��˹�Ẻ Long �������Ţ�ҹ 10 ����Թ 2,147,483,647 ���
    Dim Dec As Double
    
    ' ��Ǩ�ͺ�����ѡ����դ�� A - F ������� �ҡ���ͧ����¹���Ţ�ҹ 10 ��͹
    ' �� A = 10, B = 11, C = 12 , ... F = 15
    Dim Hex2Dec As Byte
    
    ' ����á�˹�����Ţ¡���ѧ�ͧ������ѡ �ҡ 0, 1, 2, 3 ... �������樹�ú�ӹǹ��ѡ�ҹ 16
    Dim HexWeight As Byte
    
    ' ������������ 0 ���е�ͧ��� 16 ¡���ѧ 0 ��� ������鹷��� 1 �����ҨФú�ӹǹ�ӹǹ��ѡ
    HexWeight = 0
    
    ' �繡�äӹdz�Ҩҡ��ѡ�������ش 价ҧ��ѡ��������ش
    ' Ŵ��Ҥ�����Ǣͧ�Ţ�ҹ 16 ŧ������ 1 ��ѡ
    ' �»�������ѡ�ж�Ѵ��ҹ�ҡ����仢�� �������¹���������¢�� ... ����Ẻ����Ҩ���ѹ
    ' ¡������ҧ�� A3F (�դ������ = 3) ... ��ͧ�ͧ�ӡ�� Debug Program �ٴ��¹�Ф�Ѻ
    ' �ͺ�á i ����ҹ������������ 3 ��蹤�ͤ���� Mid$(Hex, 3, 1) ����ҹ�������ѡ��� 3 (1 ���) ��� F
    ' �ͺ����ͧ i �ж١Ŵ���ŧ 1 (����� 2) ��蹤�ͤ���� Mid$(Hex, 2, 1) �����ҹ������ѡ��� 2 (1 ���) ��� 3
    ' �ͺ������ i �ж١Ŵ���ŧ 1 (����� 1) ��蹤�ͤ���� Mid$(Hex, 1, 1) �����ҹ������ѡ��� 1 (1 ���) ��� A
    For i = Len(Hex) To 1 Step -1
    
        ' ���ͺ�Ҥ�ҵ���ѡ�� A - F ���᷹�Ţ�ҹ 10 �����ҧ 10 - 15 ��͹
        Select Case Mid$(Hex, i, 1)
            Case "A"
                Hex2Dec = 10
            Case "B"
                Hex2Dec = 11
            Case "C"
                Hex2Dec = 12
            Case "D"
                Hex2Dec = 13
            Case "E"
                Hex2Dec = 14
            Case "F"
                Hex2Dec = 15
            Case Else
            
                ' �ҡ�繤�� 0 - 9 �����Ңͧ����ѹ�ͧ���� (������ҡѺ �ҹ 10 ��������)
                Hex2Dec = Mid$(Hex, i, 1)
                
        End Select
        
        ' ��Ǩ�ͺ�����ѡ����� 0 ������� �ҡ������������ ���� 0 �ٳ�Ѻ���á��� 0 ����
        If Hex2Dec <> 0 Then Dec = Dec + ((16 ^ HexWeight) * Hex2Dec)
        
        ' ��������Ţ¡���ѧ����ա 1
        HexWeight = HexWeight + 1
        
    Next
    
    ' �觤�ҡ�Ѻ��ҹ���Ϳѧ���蹵���ѹ�ͧ
    HexToDecimal = Dec

End Function

' #######################################################
' ������¡��ҹ
' #######################################################
Private Sub Form_Load()
    Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2

    ' ���¡��ѧ�����¡�äӹdz�Ҥ���Ţ�ҹ 10 �ҡ������µç
    MsgBox HexToDecimal("FED0")
    
End Sub

แปลงเลขฐาน 16 เป็นตัวอักษร

Run Time

����ŧ�Ţ�ҹ 16 ���Ţ�ҹ 2 ��͹ �ҡ�ҹ 2 �������繰ҹ 10


Private Sub cmdConvert_Click()
    Dim i As Integer
    
    txtBinary.Text = ""
    txtDecimal.Text = ""
    
    If Trim$(txtHexadecimal.Text) = "" Or Len(Trim$(txtHexadecimal.Text)) = 0 Then Exit Sub
    
    ' #######################################################
    ' ����ŧ�Ţ�ҹ 16 (HexaDecimal) ���������Ţ�ҹ 2 (Binary)
    ' #######################################################
    
    ' ������º��º����Ţ�ҹ 16 ������ѡ �Ѻ�ҡ�ҧ�������ش价ҧ��������ش
    ' ���ǹѺ�����ѧ价��� 1 ��ѡ (Step - 1)
    ' �������ѡ��ç���� ��� ���º��º��Ңͧ�Ţ�ҹ 16 ������ѡ (���仫���)
    ' ��������ѡ�ͧ�Ţ�ҹ 16 ��᷹�Ţ�ҹ 2 �颹Ҵ 4 �Ե
    For i = Len(txtHexadecimal.Text) To 1 Step -1
        Select Case Mid$(txtHexadecimal, i, 1)
            Case "0"
                txtBinary.Text = "0000" & txtBinary.Text
            Case "1"
                txtBinary.Text = "0001" & txtBinary.Text
            Case "2"
                txtBinary.Text = "0010" & txtBinary.Text
            Case "3"
                txtBinary.Text = "0011" & txtBinary.Text
            Case "4"
                txtBinary.Text = "0100" & txtBinary.Text
            Case "5"
                txtBinary.Text = "0101" & txtBinary.Text
            Case "6"
                txtBinary.Text = "0110" & txtBinary.Text
            Case "7"
                txtBinary.Text = "0111" & txtBinary.Text
            Case "8"
                txtBinary.Text = "1000" & txtBinary.Text
            Case "9"
                txtBinary.Text = "1001" & txtBinary.Text
            Case "A"
                txtBinary.Text = "1010" & txtBinary.Text
            Case "B"
                txtBinary.Text = "1011" & txtBinary.Text
            Case "C"
                txtBinary.Text = "1100" & txtBinary.Text
            Case "D"
                txtBinary.Text = "1101" & txtBinary.Text
            Case "E"
                txtBinary.Text = "1110" & txtBinary.Text
            Case "F"
                txtBinary.Text = "1111" & txtBinary.Text
        End Select
    Next
    
    ' #######################################################
    ' ����ŧ�Ţ�ҹ 2 (Binary) ���������Ţ�ҹ 10 (Decimal)
    ' #######################################################
    ' �纤���Ţ�ҹ 10 �繵����Ẻ Double �բ�Ҵ 8 亵�
    ' �ҡ��˹�Ẻ Long �������Ţ�ҹ 10 ����Թ 2,147,483,647 ��� ...
    ' �ҡ��һ�͹��Ұҹ 16 = FFFFFFFF �з�����Դ Overflow (�դ���Թ������è��Ѻ��)
    Dim Dec As Double
    
    ' ����á�˹�����Ţ¡���ѧ�ͧ�Ե������ѡ �ҡ 0, 1, 2, 3 ... �������樹�ú�ӹǹ�Ե
    Dim BitWeight As Integer
    ' ������������ 0 ���е�ͧ��� 2 ¡���ѧ 0 ��� ������鹷��� 1 �����ҨФú�ӹǹ�Ե
    'BitWeight = 0
    
    ' �繡�äӹdz�Ҩҡ�Ե�������ش (LSB - Least Significant Bit ���� �Ե����Ӥѭ����ش)
    ' 价ҧ�Ե��������ش (MSB - Most Significant Bit ���� �Ե����Ӥѭ�٧�ش)
    ' Ŵ��Ҥ�����Ǩҡ�ҧ��Ңͧ�Ţ Binary �ҷҧ����ŧ������ 1
    For i = Len(txtBinary.Text) To 1 Step -1
        ' �ҹ��˹ѡ��Ш���ѡ�ҡ�Ե�������ش (LSB) ��ѧ�Ե��������ش (MSB)
        ' �¡����� 2 ¡���ѧ����� 0 ��� ������Ң�� 1 �������� �����ҨФú�ӹǹ�Ե
        ' �ҡ��鹤ٳ��ҡѺ��ҺԵ ��蹤�� ��� 0 ���� 1 ��ҹ�� �������������кԵ����������
        ' �� 1011 ����� (2^3 x 1) + (2^2 x 0) + (2^1 x 1) + (2^0 x 1)
        
        ' Ẻ����繡�äӹdz������ء�Ե
        ' Dec = Dec + ((2 ^ BitWeight) * Mid$(txtBinary.Text, i, 1))
        
        ' ���� ��Ǩ�ͺ��ҺԵ����� 0 ������� �ҡ������������ ���� 0 �ٳ�Ѻ���á��� 0 ����
        If Mid$(txtBinary.Text, i, 1) <> 0 Then Dec = Dec + ((2 ^ BitWeight) * Mid$(txtBinary.Text, i, 1))
        
        ' ��������Ţ¡���ѧ����ա 1
        BitWeight = BitWeight + 1
    Next
    
    ' #######################################################
    ' �óշ������������ ������˹ѡ�ͧ���кԵ�ٳ 2 ������ͧ�ҡ�Ե���仫��� ��
    ' �ٳ 2 ������������ ... 1024 512 256 128 64 32 16 8 4 2 1
    'BitWeight = 1
    'For i = Len(txtBinary.Text) To 1 Step -1
    '    Dec = Dec + (BitWeight * Mid$(txtBinary.Text, i, 1))
    '    BitWeight = BitWeight * 2
    'Next
    ' #######################################################
    
    ' �ʴ����Ţ�ҹ 10 � TextBox
    txtDecimal.Text = Dec
    
    ' ���¡��ѧ�����¡�äӹdz�Ҥ���Ţ�ҹ 10 �ҡ������µç
    MsgBox HexToDecimal(txtHexadecimal.Text)
    
End Sub

Private Sub Form_Load()
    Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
    
    txtHexadecimal.Text = ""
    txtBinary.Text = ""
    txtDecimal.Text = ""
    
End Sub

Private Sub txtHexadecimal_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
        KeyAscii = 0
        Call cmdConvert_Click
    Else
        ' ��Ǩ�ͺ��á���������Ѻ��੾�Ф�� 0 - 9 ��� A - F ��ҹ��
        KeyAscii = CheckHexaDecimal(KeyAscii)
    End If
End Sub

' #######################################################
' �ѧ���蹵�Ǩ�ͺ��á���������Ѻ��੾�Ф�� 0 - 9 ��� A - F ��ҹ��
' #######################################################
Function CheckHexaDecimal(Index As Integer)
    Select Case Index
        ' ASCII Code 48 - 57 ��� ����ѡ��� 0 - 9
        ' ASCII Code 65 - 70 ��� ����ѡ��� A - F
        Case 48 To 57, 65 To 70
        Case 8, 13
        Case Else
            Index = 0
    End Select
    CheckHexaDecimal = Index
End Function

Conclusion:
�µ������������ǹ�ҡ���������Ң�����㹡���ŧ�Ţ�ҹ��ҧ� ����⨷���ú�ҹ�ѹ᷺��駹�����Ф�Ѻ ��觻���㹪��Ե��ԧ�͡�ʷ����Ҩ����Ţ�ҹ������� �ҹ 10 �ҹ 2 ��� �ҹ 16 ��ҹ�� ��ǹ��������¹�ҷҧ Hardware ���� ��¹���� Assembly ���ѡ������Ǣ�ͧ�Ѻ�Ţ�ҹ 2 ��� �ҹ 16 ��ҹ�� ��������ҹ 10 �ѧ��鹺����������֧���¡�����繶֧����ŧ�Ţ�ҹ 16 ����Ţ�ҹ 2 ��͹
    ����¡������ҧ�ͧ�����¹���� Assembly �Ѻ������������ Z80 ��Ҵ 8 �Ե - ����觢ͧ��úǡ (ADD)
  • LD A, 3F    ; ����繡�á�˹���� 3F (�ҹ 16 ��Ҵ 8 �Ե) ���Ѻ Register A (�����дѺ�٧��͡�á�˹�����������)
    LD B, 29    ; ����繡�á�˹���� 29 (�ҹ 16 ��Ҵ 8 �Ե) ���Ѻ Register B
    ADD A, B   ; ��ùӤ��� Register A �ǡ�Ѻ���� Register B ���ǹӼ��Ѿ���Ѻ��纷�� Register A (Ẻ���Դ��ҵ�Ƿ��ͧ��ѡ��������ش)
    Register ��� ˹��¤����Ӫ�Դ˹�觷���������㹵�� CPU ������� ��������
      �Ըա�äԴ (������� ��� ��) �ѧ���͡���ա��ѡ� 2 �Ը�
    • ��Ҥ�Ұҹ 16 ��� 3F �ǡ 29 �µç����
      3 F +
      2 9
      6 8 <--- �ӵͺ
      ��蹤����� F ���� 15 (�ҹ 10) �ǡ��ҡѺ 9 = 24 ��ҷ�����ѹ�Թ�Ţ�ҹ 16 �����ź�͡�ҡ�Ţ�ҹ ��� 16 (24 - 16 = ��� 8 �� 1) ... ��ѡ���� 3 + 2 + 1 (��Ƿ�) = 68 ... �������¤�Ѻ
      ����ѹ�����Դ��ҧ仨ҡ�Ţ�ҹ 10 ��� �� 29 + 5 ... ������ 9 + 5 = 14 ... 14 �ѹ�Թ��Ңͧ�Ţ�ҹ 10 �ѧ��鹵�ͧ��� 14 - 10 = 4 ��� 1 (�繵�Ƿ�) ... ������� 2 + 1 (��Ƿ�) ... ���Ѿ��ӵͺ��� 34 ����ͧ ... ������¤�Ѻ����ԸդԴ�ѹ��ҧ�ҡ�Ѻ�Ţ�ҹ 10 �ç�˹���
    • �ŧ�Ţ�ҹ 16 ������ѡ��������Ţ�ҹ 2 ��͹���Ǥ����ŧ��Ѻ���Ţ�ҹ 16 �ա��
      3 F = 0011 1111 + (�ҹ 16 ���е�� ��᷹�Ţ Binary ��Ҵ 4 �Ե)
      2 9 = 0010 1001
      --> = 0110 1000 <-- �ŧ���Ţ�ҹ 16 ����
      --> =   6      8
    ͹�� ... 㹡�����Ţ�ҹ 2 �ѹ�դ������繵�͡�öʹ���ʤ���� (Opcode) ��͹���¹�Ф�Ѻ��Ҩ�����ѹ�ӧҹ���� ... ������¼����¹���Ф������ҵ�ͧ�ʹ���ʡѹ������� ���ؤ���¹���� Assembler ���� Simulator �����ҹ����¢�� ... ����������ѹ���¹������ �֧��������������� �Ҵ��鹰ҹ�����