�ѧࡵ��� ��äӹdz�Ҥ�ҹ��˹ѡ (Weight) ��Ш���ѡ ��������Ţ�ҹ���� ¡���ѧ����� 0 (�ҧ�������ش) ������������Ţ¡���ѧ价ҧ������͢��令����� 1 �������� �����Ҩ�����ش��ѡ�ҧ������� ... (���й�����Ф����ѡ��÷����ҵ�ͧ��������¹�������)
�������Ѵਹ�ҡ�Ţ��� 3 �ҹ������ ��ѡ��÷������� (����������ѹ�������) 㹡���ŧ�Ţ�ҹ������Ţ�ҹ 10 �ѹ���������¹���� ��觷������¹ ��� �Ţ�ҹ�����¡���ѧ �����Ҥ�ҹ��˹ѡ (Weight) ��Ш���ѡ ��Ш�¡����Ţ�ҹ 16 �������ͧ�ҡ��ҵ������ 10 - 15 ����Ţ�ѹ�բ�Ҵ 2 ��ѡ �֧����дǡ��͡�ù�����ҹ �ҧ���Դ�к��Ţ�ҹ �֧��Ӥ�� A - F ��᷹��ҷ����Ҵѧ����� �ѧ��鹡������ŧ�Ţ�ҹ 16 ����Ţ�ҹ 10 �е�ͧ�ա�õ�Ǩ�ͺ���¡������ѡ���� �դ�� A - F ����������� ... �ҡ�յ�ͧ�ŧ�������������Ţ�ҹ 10 ����ӹǹ�ͧ�ѹ�ͧ�� (�Ԩ�óҨҡ���ҧ��ҧ��ҧ��Сͺ)
HEXADECIMAL(�Ţ�ҹ 16 ���е�Ǩ��� Binary ��Ҵ 4 �Ե)
DECIMAL(�����Ţ�ҹ 10)
2^3 = 8
2^2 = 4
2^1 = 2
2^0 = 1
0
1
2
3
4
5
6
7
8
9
A
10
B
11
C
12
D
13
E
14
F
15
' ####################################################### ' �ѧ���蹡���ŧ�Ţ�ҹ 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
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