1、VB 编程序实现十进制数转换成 2、8、16 进制数十进制转二进制Public Function DECtoBIN(Dec As Long) As StringDECtoBIN = “Do While Dec 0DECtoBIN = Dec Mod 2 DecDores = Dec Mod 2 求出除以 2 的余数Bin = res BinPrivate Sub Command1_Click()Print bin(Text1.Text)End SubFunction bin(n As Long) As StringDo Until n = 0bin = (n Mod 2) & binn = n
2、 2LoopEnd Function十六进制:Hex(number) 或使用&H 前缀 八进制:Oct(number)或使用&O 前缀 二、八、十六进制转为十进制:参数 sdate 为要进行转换的数,stype 为此数的类型。如要将十六进制的“7b“转为十进制 : msgbox OtherToShi(“7b“,16)Private Function OtherToShi(ByVal Sdate As String, ByVal Stype As Long) As StringDim A As String, K As Long, P As LongIf trim(sdate)=“ then m
3、sgbox “请输入要转换的数!“ :Exit functionOn Error GoTo ExitsubP = Len(Sdate)Select Case StypeCase 2For K = 1 To PIf Mid(Sdate, K, 1) 1 Then GoTo ExitsubNextCase 8If IsNumeric(Sdate) = False Then GoTo ExitsubSdate = Round(Sdate)p=len(sdate)Case 16For K = 1 To PA = Asc(LCase(Mid(Sdate, K, 1)If A 49 And A 102 T
4、hen GoTo ExitsubNextCase Else 按你说的操作,此句好象可免MsgBox “指定转换的类型不正确,请重新输入!“: Exit FunctionEnd SelectK = 0Do While K 0S = LTrim(Str(Sdate Mod Stype)If Stype = 16 ThenSelect Case SCase “10“S = “A“Case “11“S = “B“Case “12“S = “C“Case “13“S = “D“Case “14“S = “E“Case “15“S = “F“End SelectEnd IfShiToOther = S & ShiToOtherSdate = Sdate StypeLoopExit FunctionExitsub:MsgBox “要转换的数据非十进制数据,请重新输入“End Function