把字符串“12341168C5B7”轉(zhuǎn)換為16進(jìn)制數(shù)存為string型
成都創(chuàng)新互聯(lián)公司 - 服務(wù)器托管,四川服務(wù)器租用,成都服務(wù)器租用,四川網(wǎng)通托管,綿陽(yáng)服務(wù)器托管,德陽(yáng)服務(wù)器托管,遂寧服務(wù)器托管,綿陽(yáng)服務(wù)器托管,四川云主機(jī),成都云主機(jī),西南云主機(jī),服務(wù)器托管,西南服務(wù)器托管,四川/成都大帶寬,機(jī)柜大帶寬,四川老牌IDC服務(wù)商
用shuzi=Convert.ToInt32("12341168C5B7",16)
就ok了。
反之用 shuzi.ToString("X")就行了。
給分?。?!
12341168C5B7轉(zhuǎn)換成10進(jìn)制數(shù)就是20014839678391。。
而int32的最大值為2,147,483,647。。所以產(chǎn)生錯(cuò)誤。
所以要用int64,它的最大值為9,223,372,036,854,775,807。也就是16位十六進(jìn)制數(shù)0X7FFFFFFFFFFFFFFF。
難道還有比F靠后的數(shù)??????
VB.Net自帶函數(shù):
十進(jìn)制轉(zhuǎn)八進(jìn)制:Oct(num)
十六進(jìn)制轉(zhuǎn)八進(jìn)制:oct("H" num)
十進(jìn)制轉(zhuǎn)十六進(jìn)制:hex(num)
八進(jìn)制轉(zhuǎn)十六進(jìn)制:hex("O" num)
十六進(jìn)制轉(zhuǎn)換為十進(jìn)制:CLng("H" num)
Private?Sub?Form1_Load(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?MyBase.Load
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
10進(jìn)制轉(zhuǎn)16進(jìn)制的話可以使用HEX()函數(shù),16進(jìn)制轉(zhuǎn)10進(jìn)制沒(méi)有現(xiàn)成函數(shù),可以使用如下代碼實(shí)現(xiàn):
'?用途:將十六進(jìn)制轉(zhuǎn)化為十進(jìn)制
'?輸入:Hex(十六進(jìn)制數(shù))
'?輸入數(shù)據(jù)類型:String
'?輸出:HEX_to_DEC(十進(jìn)制數(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
擴(kuò)展資料:
在VB中將十進(jìn)制轉(zhuǎn)化為十六進(jìn)制的函數(shù):
輸入:Dec(十進(jìn)制數(shù))'
輸入數(shù)據(jù)類型:Long'
輸出:D2H(十六進(jìn)制數(shù))'
輸出數(shù)據(jù)類型:String'
輸入的最大數(shù)為2147483647,
輸出最大數(shù)為7FFFFFFFPublicFunction D2H(Dec As Long) As String
Dim a As String
D2H = ""
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
D2H = a D2H
Dec = Dec \ 16 ? ? LoopEnd Function