Private?Sub?Form1_Load(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?MyBase.Load
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),金昌企業(yè)網(wǎng)站建設(shè),金昌品牌網(wǎng)站建設(shè),網(wǎng)站定制,金昌網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,金昌網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
MsgBox(DEC_to_HEX(255))
End?Sub
Public?Function?DEC_to_HEX(ByVal?Dec?As?Long)?As?String
Dim?a?As?String
DEC_to_HEX?=?""
Do?While?Dec??0
a?=?CStr(Dec?Mod?16)
Select?Case?a
Case?"10"?:?a?=?"A"
Case?"11"?:?a?=?"B"
Case?"12"?:?a?=?"C"
Case?"13"?:?a?=?"D"
Case?"14"?:?a?=?"E"
Case?"15"?:?a?=?"F"
End?Select
DEC_to_HEX?=?a??DEC_to_HEX
Dec?=?Dec?\?16
Loop
End?Function
把字符串“12341168C5B7”轉(zhuǎn)換為16進制數(shù)存為string型
用shuzi=Convert.ToInt32("12341168C5B7",16)
就ok了。
反之用 shuzi.ToString("X")就行了。
給分?。?!
12341168C5B7轉(zhuǎn)換成10進制數(shù)就是20014839678391。。
而int32的最大值為2,147,483,647。。所以產(chǎn)生錯誤。
所以要用int64,它的最大值為9,223,372,036,854,775,807。也就是16位十六進制數(shù)0X7FFFFFFFFFFFFFFF。
難道還有比F靠后的數(shù)??????
'此函數(shù)用于十進制到二進制
Private Function Bin(Number As Long) As Long
Dim Div_value As String
Do
Div_value = (Number Mod 2) Div_value
Number = Int(Number / 2)
Loop Until Number 2
Bin = Number Div_value
End Function
'此函數(shù)用于二進制到十進制
Private Function value_Bin(Number As Long) As Long
Dim Temp_value As Long, Cyc As Long
For Cyc = 0 To Len(Str(Number))
If Cyc = Len(Str(Number)) Then
value_Bin = Temp_value + Val(Mid(Str(Number), Cyc, 1))
Exit For
End If
If Cyc 0 Then Temp_value = (Temp_value + Val(Mid(Str(Number), Cyc, 1))) * 2
Next Cyc
End Function
'====寫入以上函數(shù)后轉(zhuǎn)值操作:
'十進制到十六進制:Hex(十進制數(shù)值)
'十進制到八進制:Oct(十進制數(shù)值)
'十進制到二進制:Bin(十進制數(shù)值)
'二進制到十進制:value_Bin(二進制數(shù)值)
'八進制到十進制:Val("O" 八進制數(shù)值)
'十六進制到十進制:Val("H" 十六進制數(shù)值)
比如
msgbox DEC_to_BIN(10)
就會返回一個10轉(zhuǎn)換為十進制后的結(jié)果
或者簡單點就用
VB自帶函數(shù):
十進制轉(zhuǎn)八進制:Oct(num)
十六進制轉(zhuǎn)八進制:oct("H" num)
十進制轉(zhuǎn)十六進制:hex(num)
八進制轉(zhuǎn)十六進制:hex("O" num)
十六進制轉(zhuǎn)換為十進制
Dim str As String
str = Text2.Text
Text10.Text = CLng("H" str)
numHex =Integer.Parse(strHex, Globalization.NumberStyles.AllowHexSpecifier)
或
numHex = Integer. Parse("H" strHex)
你不用理解的 只要將要轉(zhuǎn)換的參數(shù)傳入就可以的了
比如
msgbox DEC_to_BIN(10)
就會返回一個10轉(zhuǎn)換為十進制后的結(jié)果
或者簡單點就用
VB自帶函數(shù):
十進制轉(zhuǎn)八進制:Oct(num)
十六進制轉(zhuǎn)八進制:oct("H" num)
十進制轉(zhuǎn)十六進制:hex(num)
八進制轉(zhuǎn)十六進制:hex("O" num)
十六進制轉(zhuǎn)換為十進制
Dim str As String
str = Text2.Text
Text10.Text = CLng("H" str)
===========================================
' 用途:將十進制轉(zhuǎn)化為二進制
' 輸入:Dec(十進制數(shù))
' 輸入數(shù)據(jù)類型:Long
' 輸出:DEC_to_BIN(二進制數(shù))
' 輸出數(shù)據(jù)類型:String
' 輸入的最大數(shù)為2147483647,輸出最大數(shù)為1111111111111111111111111111111(31個1)
Public Function DEC_to_BIN(Dec As Long) As String
DEC_to_BIN = ""
Do While Dec 0
DEC_to_BIN = Dec Mod 2 DEC_to_BIN
Dec = Dec \ 2
Loop
End Function
' 用途:將二進制轉(zhuǎn)化為十進制
' 輸入:Bin(二進制數(shù))
' 輸入數(shù)據(jù)類型:String
' 輸出:BIN_to_DEC(十進制數(shù))
' 輸出數(shù)據(jù)類型:Long
' 輸入的最大數(shù)為1111111111111111111111111111111(31個1),輸出最大數(shù)為2147483647
Public Function BIN_to_DEC(ByVal Bin As String) As Long
Dim i As Long
For i = 1 To Len(Bin)
BIN_to_DEC = BIN_to_DEC * 2 + Val(Mid(Bin, i, 1))
Next i
End Function
' 用途:將十六進制轉(zhuǎn)化為二進制
' 輸入:Hex(十六進制數(shù))
' 輸入數(shù)據(jù)類型:String
' 輸出:HEX_to_BIN(二進制數(shù))
' 輸出數(shù)據(jù)類型:String
' 輸入的最大數(shù)為2147483647個字符
Public Function HEX_to_BIN(ByVal Hex As String) As String
Dim i As Long
Dim B As String
Hex = UCase(Hex)
For i = 1 To Len(Hex)
Select Case Mid(Hex, i, 1)
Case "0": B = B "0000"
Case "1": B = B "0001"
Case "2": B = B "0010"
Case "3": B = B "0011"
Case "4": B = B "0100"
Case "5": B = B "0101"
Case "6": B = B "0110"
Case "7": B = B "0111"
Case "8": B = B "1000"
Case "9": B = B "1001"
Case "A": B = B "1010"
Case "B": B = B "1011"
Case "C": B = B "1100"
Case "D": B = B "1101"
Case "E": B = B "1110"
Case "F": B = B "1111"
End Select
Next i
While Left(B, 1) = "0"
B = Right(B, Len(B) - 1)
Wend
HEX_to_BIN = B
End Function
' 用途:將二進制轉(zhuǎn)化為十六進制
' 輸入:Bin(二進制數(shù))
' 輸入數(shù)據(jù)類型:String
' 輸出:BIN_to_HEX(十六進制數(shù))
' 輸出數(shù)據(jù)類型:String
' 輸入的最大數(shù)為2147483647個字符
Public Function BIN_to_HEX(ByVal Bin As String) As String
Dim i As Long
Dim H As String
If Len(Bin) Mod 4 0 Then
Bin = String(4 - Len(Bin) Mod 4, "0") Bin
End If
For i = 1 To Len(Bin) Step 4
Select Case Mid(Bin, i, 4)
Case "0000": H = H "0"
Case "0001": H = H "1"
Case "0010": H = H "2"
Case "0011": H = H "3"
Case "0100": H = H "4"
Case "0101": H = H "5"
Case "0110": H = H "6"
Case "0111": H = H "7"
Case "1000": H = H "8"
Case "1001": H = H "9"
Case "1010": H = H "A"
Case "1011": H = H "B"
Case "1100": H = H "C"
Case "1101": H = H "D"
Case "1110": H = H "E"
Case "1111": H = H "F"
End Select
Next i
While Left(H, 1) = "0"
H = Right(H, Len(H) - 1)
Wend
BIN_to_HEX = H
End Function
' 用途:將十六進制轉(zhuǎn)化為十進制
' 輸入:Hex(十六進制數(shù))
' 輸入數(shù)據(jù)類型:String
' 輸出:HEX_to_DEC(十進制數(shù))
' 輸出數(shù)據(jù)類型:Long
' 輸入的最大數(shù)為7FFFFFFF,輸出的最大數(shù)為2147483647
Public Function HEX_to_DEC(ByVal Hex As String) As Long
Dim i As Long
Dim B As Long
Hex = UCase(Hex)
For i = 1 To Len(Hex)
Select Case Mid(Hex, Len(Hex) - i + 1, 1)
Case "0": B = B + 16 ^ (i - 1) * 0
Case "1": B = B + 16 ^ (i - 1) * 1
Case "2": B = B + 16 ^ (i - 1) * 2
Case "3": B = B + 16 ^ (i - 1) * 3
Case "4": B = B + 16 ^ (i - 1) * 4
Case "5": B = B + 16 ^ (i - 1) * 5
Case "6": B = B + 16 ^ (i - 1) * 6
Case "7": B = B + 16 ^ (i - 1) * 7
Case "8": B = B + 16 ^ (i - 1) * 8
Case "9": B = B + 16 ^ (i - 1) * 9
Case "A": B = B + 16 ^ (i - 1) * 10
Case "B": B = B + 16 ^ (i - 1) * 11
Case "C": B = B + 16 ^ (i - 1) * 12
Case "D": B = B + 16 ^ (i - 1) * 13
Case "E": B = B + 16 ^ (i - 1) * 14
Case "F": B = B + 16 ^ (i - 1) * 15
End Select
Next i
HEX_to_DEC = B
End Function
' 用途:將十進制轉(zhuǎn)化為十六進制
' 輸入:Dec(十進制數(shù))
' 輸入數(shù)據(jù)類型:Long
' 輸出:DEC_to_HEX(十六進制數(shù))
' 輸出數(shù)據(jù)類型:String
' 輸入的最大數(shù)為2147483647,輸出最大數(shù)為7FFFFFFF
Public Function DEC_to_HEX(Dec As Long) As String
Dim a As String
DEC_to_HEX = ""
Do While Dec 0
a = CStr(Dec Mod 16)
Select Case a
Case "10": a = "A"
Case "11": a = "B"
Case "12": a = "C"
Case "13": a = "D"
Case "14": a = "E"
Case "15": a = "F"
End Select
DEC_to_HEX = a DEC_to_HEX
Dec = Dec \ 16
Loop
End Function
' 用途:將十進制轉(zhuǎn)化為八進制
' 輸入:Dec(十進制數(shù))
' 輸入數(shù)據(jù)類型:Long
' 輸出:DEC_to_OCT(八進制數(shù))
' 輸出數(shù)據(jù)類型:String
' 輸入的最大數(shù)為2147483647,輸出最大數(shù)為17777777777
Public Function DEC_to_OCT(Dec As Long) As String
DEC_to_OCT = ""
Do While Dec 0
DEC_to_OCT = Dec Mod 8 DEC_to_OCT
Dec = Dec \ 8
Loop
End Function
' 用途:將八進制轉(zhuǎn)化為十進制
' 輸入:Oct(八進制數(shù))
' 輸入數(shù)據(jù)類型:String
' 輸出:OCT_to_DEC(十進制數(shù))
' 輸出數(shù)據(jù)類型:Long
' 輸入的最大數(shù)為17777777777,輸出的最大數(shù)為2147483647
Public Function OCT_to_DEC(ByVal Oct As String) As Long
Dim i As Long
Dim B As Long
For i = 1 To Len(Oct)
Select Case Mid(Oct, Len(Oct) - i + 1, 1)
Case "0": B = B + 8 ^ (i - 1) * 0
Case "1": B = B + 8 ^ (i - 1) * 1
Case "2": B = B + 8 ^ (i - 1) * 2
Case "3": B = B + 8 ^ (i - 1) * 3
Case "4": B = B + 8 ^ (i - 1) * 4
Case "5": B = B + 8 ^ (i - 1) * 5
Case "6": B = B + 8 ^ (i - 1) * 6
Case "7": B = B + 8 ^ (i - 1) * 7
End Select
Next i
OCT_to_DEC = B
End Function
' 用途:將二進制轉(zhuǎn)化為八進制
' 輸入:Bin(二進制數(shù))
' 輸入數(shù)據(jù)類型:String
' 輸出:BIN_to_OCT(八進制數(shù))
' 輸出數(shù)據(jù)類型:String
' 輸入的最大數(shù)為2147483647個字符
Public Function BIN_to_OCT(ByVal Bin As String) As String
Dim i As Long
Dim H As String
If Len(Bin) Mod 3 0 Then
Bin = String(3 - Len(Bin) Mod 3, "0") Bin
End If
For i = 1 To Len(Bin) Step 3
Select Case Mid(Bin, i, 3)
Case "000": H = H "0"
Case "001": H = H "1"
Case "010": H = H "2"
Case "011": H = H "3"
Case "100": H = H "4"
Case "101": H = H "5"
Case "110": H = H "6"
Case "111": H = H "7"
End Select
Next i
While Left(H, 1) = "0"
H = Right(H, Len(H) - 1)
Wend
BIN_to_OCT = H
End Function
' 用途:將八進制轉(zhuǎn)化為二進制
' 輸入:Oct(八進制數(shù))
' 輸入數(shù)據(jù)類型:String
' 輸出:OCT_to_BIN(二進制數(shù))
' 輸出數(shù)據(jù)類型:String
' 輸入的最大數(shù)為2147483647個字符
Public Function OCT_to_BIN(ByVal Oct As String) As String
Dim i As Long
Dim B As String
For i = 1 To Len(Oct)
Select Case Mid(Oct, i, 1)
Case "0": B = B "000"
Case "1": B = B "001"
Case "2": B = B "010"
Case "3": B = B "011"
Case "4": B = B "100"
Case "5": B = B "101"
Case "6": B = B "110"
Case "7": B = B "111"
End Select
Next i
While Left(B, 1) = "0"
B = Right(B, Len(B) - 1)
Wend
OCT_to_BIN = B
End Function
' 用途:將八進制轉(zhuǎn)化為十六進制
' 輸入:Oct(八進制數(shù))
' 輸入數(shù)據(jù)類型:String
' 輸出:OCT_to_HEX(十六進制數(shù))
' 輸出數(shù)據(jù)類型:String
' 輸入的最大數(shù)為2147483647個字符
Public Function OCT_to_HEX(ByVal Oct As String) As String
Dim Bin As String
Bin = OCT_to_BIN(Oct)
OCT_to_HEX = BIN_to_HEX(Bin)
End Function
' 用途:將十六進制轉(zhuǎn)化為八進制
' 輸入:Hex(十六進制數(shù))
' 輸入數(shù)據(jù)類型:String
' 輸出:HEX_to_OCT(八進制數(shù))
' 輸出數(shù)據(jù)類型:String
' 輸入的最大數(shù)為2147483647個字符
Public Function HEX_to_OCT(ByVal Hex As String) As String
Dim Bin As String
Hex = UCase(Hex)
Bin = HEX_to_BIN(Hex)
HEX_to_OCT = BIN_to_OCT(Bin)
End Function