ҡդӶ ʵŧ 纺촨շ٨絴͵ ᷹ФѺ

͵Դҷҧ Inbox Ѻ

˹ѡ
-
VB 6/VB.Net
ASP/ASP.Net
Ѻ¤
¹ҹ Flash Movie
ռͺش  
 RSS Feeds
 ǹŴ RSS Reader ...   Download  RSS Reader

Forum - www.g2gnet.com
Webmaster - www.g2gnet.com
Visitors - Session views
 5 6 1 6 1 7 0

7 ѹҤ ..2549
125 Users On-Line.
Visitors - Page views
 8 9 7 3 9 1 3
1 Ҿѹ ..2551

Google   
g2gnet.com
Ҵѡ:  

ŧŢҹ 10 Ţҹ 16 Ţҹ 2 Ţҹ 16 (ǤԴͧҧ)

Category »  VB 6/VB.Net
: Webmaster 23/11/2552   : 02:52
(ҹ : 135348) 
... Ѻ 繤͹ҧ¹ աԨ 蹴 (ҧ¹ҧФѺ) ͹һ·ҧ зҹ ... ǺҶ֧͹ .5 ֧¹ŧ鹷ҧӤѭ㹪Ե ¡ "ѧ" 1 ¹Ҫվ Ң硷͹ԡ ... ¹֡ѡ 㹡õѴԹ¹ªҧͧ ... Ф¹ ѹ١ʹյ Ѩغѹ ... ҷȹ ҹͧ餹ǹ˭ ѡͧҤªҧ Իѭѹ 觷ɮ »Ժѵ ѧçҹѡ ҡͧ ѹ ˹觢ͧ診ҧ ٨ ·᷺ͧҺáѹҡ¹ѡ ... ش֧͹ʹǤԴ ѡСûԺѵ ͹ҹ (ԧ) ... ͧҡҢ硷͹ԡͧ¹Ԫ᡹ѡҧ ǧôԨԵ (ԧ /áʵ) Ԫ (֡÷ӧҹͧ) 2 Ԫҹ ǹӷҧö¹ Assembly C ҧͧҵ ...

ŧŢҹ 10 Ţҹ 16 (Visual Basic 6)
  • Dim X as String
  • X = Hex$(127)
    ... 55555+ ...
ҡóշ ѹդ (Ϳѧ) Ǩзҧô ... Ѻ () ... 硴ͧŢҫԤѺ Windows ѹ ( ... ѧ ) ... 55555+ ... õͺẺ ҹͧ¹ФѺ ... ѭҡǤԴẺŧŢҹ 10 Ţҹ 2 ѧ䧡ѧ¤Ѻ ... ͧ ... ¤ԴẺ ... (꡷ ... ҹ´)
  • ҹ 10 繵ǵ Ţҹͧ繵 ( 2 16)
      2 Ẻ
    • (MOD) Ѿ͡繤ӵͺ
    • õѴ (Div) ҨзѾش 0

ŢҹԺ

Ţҹͧ

ŢҹԺˡ

0

0000

0

1

0001

1

2

0010

2

3

0011

3

4

0100

4

5

0101

5

6

0110

6

7

0111

7

8

1000

8

9

1001

9

10

1010

A

11

1011

B

12

1100

C

13

1101

D

14

1110

E

15

1111

F

ͧҡ 10 - 15 (ҹ 16) ѹյŢ (Digit) Ҵ 2 ѡ ֧㹡ҹ ... (Դ) ֧ѡ (Character) A - F ᷹ӹǹѧǷҤѺ ... ͧ
    ŢҹȨ ... Ţҹդҵش = 0 Шդ٧شҡѺҹ - 1
  • Ţҹ 10 դ 9 繤٧ش (10 - 1 = 9)
  • Ţҹ 2 դ 1 繤٧ش (2 - 1 = 1)
  • Ţҹ 8 դ 7 繤٧ش (8 - 1 = 7)
  • Ţҹ 16 դ 15 繤٧ش (16 - 1 = 15)
    Ǣͧ ԴͧѹҧйФѺ ... 55555+
ѧ 㹡ŧŢҹ 10 繰ҹ 16 Ըա

' ==============================================================
Function Decimal2HexaDecimal(Dec As Long) As String
' ==============================================================
Dim Hex As String
    ' ͹ش Ө Dec = 0 騺кǹ (True) ͡ҡǧͺ Do ... While
    Do While (Dec > 0)
        ' Mod  ٧ش () ͧ Mod ź 1 
        ' ѧ 16 (ҹ 10)  Mod ٧ش (16 - 1) = 15 ͧ
        ' ͹㹡ôѡҵ 10 Թ 15  10 ᷹Ҵ A (ҹ 16)
        ' 11 = B (ҹ 16) 樹 15 = F (ҹ 16)
        Select Case (Dec Mod 16)
            Case 10
                Hex = "A" & Hex
            Case 11
                Hex = "B" & Hex
            Case 12
                Hex = "C" & Hex
            Case 13
                Hex = "D" & Hex
            Case 14
                Hex = "E" & Hex
            Case 15
                Hex = "F" & Hex
            
            '  0 - 9 һżŷçѺ ... (Ѻͧ Mod  16 ѧ ѹդԹ 15 ͡)
            ' ѧࡵ¤Ѻ ͹䢤ҧ 0 - 9 ҧ Case Else .. (ѹդա).
            Case Else
                
                ' 纤Ҥӵͺͧ (MOD) ... §ӴѺ١ͧ¹ФѺ
                Hex = (Dec Mod 16) & Hex
                
        End Select
        
        ' õѴ (Div) Ҩ 0 ... ͹㹡ǹͺ Do ... While
        Dec = Dec \ 16
    
    Loop

    ' 觤Ҥ׹ѺẺ String Ţҹ 16 ѹյѡ A - F
    Decimal2HexaDecimal = Hex
    
End Function
ǤԴẺҹѧѺ ҤسŧŢҹ 10 Ţҹ Algorithm ѡ͹ѹ ... ФѺẺѺҧ (ѹ) ͧ ... мҧԧٵ ¡ѧ Ǣͧ ѭ⨷͹¤ͧ (дѺж) չѧ¹鹰ҹѹҷ ... ¹ͻءФѺ ... ѧѺѧ ѧա ҡѹѴ觡ҹ ǤԴҹͧҧ硷͹ԡ줹˹ ŧŢҹ 2 Ţҹ 16 ҧ ...
ǹŴ
ǹŴ Source Code Ѻ MS Visual Basic 6.0 - Service Pack 6
 ǹŴ Visual Basic 6.0 SP5: Run-Time Redistribution Pack
 ǹŴ Microsoft Data Access Object (MDAC) Jet 4.0 Update
 ǹŴ Microsoft Visual Basic Service Pack 6

ԸաäԴѺŢӹǹ
ŧŢҹ 16 Ţҹ 2 Ţҹ 10 - VB6
ǤԴŧŢҹ 10 繰ҹ 2 ͧ ... ᷺͡ѹ
ͧͧ (ա) ... Sub Program, Routine, Procedure Function
Ǩͺá鹤Ѻ੾еŢ شȹҹ (VB6)
Ǩͺá鹤Ѻ੾еŢ (VB.Net)


Design Time


Run Time

ҡҧҹ ... ѧࡵФѺ Ţҹ 16 ö᷹ѹŢ Binary (ҹ 2) ӹǹҴ 4 Ե
    ҡѹ Ţ Binary ӹǹҴ 4 Ե ö᷹Ţҹ 16 úʹ ...
  • 1 x 23 + 1 x 22 + 1 x 21 + 1 x 20 ... Ծѹ繷յͧҢش¡ѹͧФѺ мСǶ֧ùŢ Binary 4 Ե ŧ繰ҹ 16
  • 0  0  0  0 = 0
  • 0  0  0  1 = 1
  • 0  0  1  0 = 2
  • 0  1  0  0 = 4
  • ...
  • 1  1  1  0 = E ( 14 ҹ 10)
  • 1  1  1  1 = F ( 15 ҹ 10)

ͤԴѧ ... ШѴŢ Binary (ҹ 2) ͡繪ش 4 Ե Ѻҡ仫 (ǧͺ For) ... Ըա ҹҵ˹觢ͧԵش ͧءش ҹҵ˹觨ҡ仢᷹ (͹ IF Else )
    Ţ 257 (ҹ 10) СШŢ Binary 1 0 0 0 0 0 0 0 1
  • 1 0 0 0 0 0 0 0 1 ... 觪شŢ Binary ش 4 Ե
  • 1 2 3 4 5 6 7 8 9 ... Ҥӹdzҵ˹觢ͧԵش Ъش оԨóѡ 6 ѡ 2 (ҹ˹觨ҡ仢)
      ͧ٨ҡ鴴ҹҧСͺ 257 (ҹ 10) 繤ҷͺ
    • ͺ 1 For Count = Len(BinaryBase) To 1 Step -4 ... Count դҡѺ 9 (ǢͧӹǹԵ Len(BinaryBase))
    • Bit = Mid$(BinaryBase, Count - 3, 4) Mid$("100000001", 9 - 3 = 6, 4) = 0001 (Ե 0 ᴧ ͵˹觷Ѻҡҧ 6 ˹ ... ҡѺ价ҧա 4 Ե
    • ͺ 2 For Count = Len(BinaryBase) To 1 Step -4 ... Count Ŵŧ 4 (Step -4) ֧դҡѺ 9 - 4 = 5
    • Bit = Mid$(BinaryBase, Count - 3, 4) Mid$("100000000", 5 - 3 = 2, 4) = 0000 (Ե 0 ᴧ ͵˹觷Ѻҡҧ 2 ˹ ... ҡѺ价ҧա 4 Ե
  • 1 ... óբͧشش ҡպԵú 4 Ե ... ԸաԵ 0 ú (͹ IF 繨ԧ)
      ...
    • ѺҴԵú 4 Ե ҺԵ§ѹ ǹѺҨҡҧҤ׹Ѻ 4 ...
    • "0000" & "1" ... ԡ§Ե ѧ "00001"
    • Right("0000" & "1", 4) ... Ѻҡҧ 4 ѡ ... ӵͺ "0001"
觵ҧ繷ҧҹ äԴ͡Ẻ ͧͺŧ㹡д ҹѺ¹ѧҹ͡Ҵ ... ҡҧ ҡ¹͡ռѾ١ͧ ... աѺԴҴշҧҹ ...

ŧŢҹ 2 Ţҹ 16 ... ԸաùѺӹǹԵ 4 Ե

' ==============================================================
' ŧŢҹ 2 Ţҹ 16 ... ·ӡäԴŢҹ 2  4 Ե
Function Binary2HexaDecimal(BinaryBase As String) As String
' ==============================================================

Dim Count As Integer    ' ѺӹǹԵ 4 Ե
Dim Bit As String           ' ŧŢҹ 16
Dim Hex As String         ' 纤ҷŧŢҹ 16 ͹¹ҹ
    
    
    ' ͡繪ش 4 Ե ͧѺҡҧҹФѺ (LSB)
    ' 㹰ҹ 10 ͹ҡѡѹ ѡ ѡԺ ѡ˹ (ԪԵ¡ѺŢ)
    ' Count Ŵŧ 4 Ѻ ... ѺӹǹǢͧӹǹԵ Ŵŧ 4 Ե
    For Count = Len(BinaryBase) To 1 Step -4
        
        ' óպ׵ҧѹբҴú 4 Ե ... ¡ 0 仫פѺ
        '  1 0000 0001 ...
        ' ǹͺش з 1 繺Եش (MSB) ͵ⴴ
        ' 㹡óշҨӹǹԵ 4, 8, 12, 16, 20,  ...  (ô 4 ŧǾʹ)
        If Count < 4 Then
            ' ===========================================================
            ' ѺҴԵú 4 Ե ҺԵ§ѹ ǹѺҨҡҧҤ׹Ѻ 4  ...
            ' "0000" & "1" ... ԡ§Ե ѧ  "00001"
            '  Right("0000" & "1", 4) ... ӵͺ  "0001"
            Bit = Right$("0000" & Mid$(BinaryBase, 1, Count), 4)
            ' ===========================================================
        
        Else
            
            ' ===========================================================
            ' ԸաäԴФѺ ...
            ' صҤ 257 (ҹ 10)   1 0 0 0 0 0 0 0 1 (ҹ 2)
            ' Ƿ 9 Ե (Count = 9) ... ź͡ 3  ...
            ' 1 2 3 4 5 6 7 8 9 (Ѻѡ)
            ' 1 0 0 0 0 0 0 0 1
            ' -----------x------- ... ͧõѴԵ 3 Ե͡ ֧ź˹ x ʹ
            ' ѧͺá 9 - 3 = 6 ... ͧѺ٤ѺҨҹҡѡ 6 ʹ Ѻҡ͹ФѺ
            ' شӤѭҡѺ ... ÷ӧҹ¡á F8 ͺ
            '  Mid$(ͤ, 鹨ҡѡ, Ѻ 4 ) ...
            ' ѹըӹǹԵ ջѭҡѺǤԴͧʹյҧ (ѹ) Ẻ ...
            Bit = Mid$(BinaryBase, Count - 3, 4)
            ' ===========================================================
        
        End If
        
        ' Ҥú 4 Եǡӡºº
        Select Case Bit
            Case "0000"
                Hex = "0"
            Case "0001"
                Hex = "1"
            Case "0010"
                Hex = "2"
            Case "0011"
                Hex = "3"
            Case "0100"
                Hex = "4"
            Case "0101"
                Hex = "5"
            Case "0110"
                Hex = "6"
            Case "0111"
                Hex = "7"
            Case "1000"
                Hex = "8"
            Case "1001"
                Hex = "9"
            Case "1010"
                Hex = "A"
            Case "1011"
                Hex = "B"
            Case "1100"
                Hex = "C"
            Case "1101"
                Hex = "D"
            Case "1110"
                Hex = "E"
            Case "1111"
                Hex = "F"
        End Select
        
        ' Ѵ§ʴ  觤Ҥ׹Ѻ (Ẻ String)
        Binary2HexaDecimal = Hex & Binary2HexaDecimal
        
    Next
    
End Function
    

Ըա Debug Program
Conclusion:
Ѻ ... ѹҡ ... ੾͹ Bit = Mid$(BinaryBase, Count - 3, 4) ... м¹ Flow Chart ͡ 觾ͧͧ֡ԴԨóҵ仴¤Ѻ ѹԸա¹͡Ẻ ѡ˹仾鹵ҧ (ͧԧ) ͡Ѻ §ǺѴӤѭ ⨷͹ǡдѺ֡Ѵ (͹ѡٵẺ ੾ŧŢҹ 蹡ѹѧ) ... ͧҡس͡ẺŧдҫС͹ ҡ¹͡Ѻ ... ǤԴ ¹Ҩҡ C ҡ͹ Ѻ 10 һ ӧҹǡѹ Algorithm ᷺ǡѺ¤Ѻ ... ͧ ... 繷ҡաҧ ѡäԴҧŢǢͧ ... ¡ҹԧǹѺ ... ͧ
͵ - g2gNet Dot Com
Ţ¹ҳԪ硷͹ԡ 0407314800231
CopyLeft © 2004 - 2099 g2gNet.Com All rights reserved.
Email: [email protected] . 08-6862-6560