VB各种进制相互转换大全

更新时间:2023-12-14 06:55:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

VB各种进制相互转换大全

源码2007-03-06 18:39挺全的十进制,八进制,十六进制,二进制相互转换都有了 -------------------------------------------------

' 用途:将十进制转化为二进制 ' 输入:Dec(十进制数) ' 输入数据类型:Long ' 输出:DEC_to_BIN(二进制数) ' 输出数据类型:String

' 输入的最大数为2147483647,输出最大数为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

' 用途:将二进制转化为十进制 ' 输入:Bin(二进制数)

' 输入数据类型:String

' 输出:BIN_to_DEC(十进制数) ' 输出数据类型:Long

' 输入的最大数为1111111111111111111111111111111(31个1),输出最大数为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

' 用途:将十六进制转化为二进制 ' 输入:Hex(十六进制数) ' 输入数据类型:String ' 输出:HEX_to_BIN(二进制数) ' 输出数据类型:String

' 输入的最大数为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 \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ End Select Next i

While Left(B, 1) = \ B = Right(B, Len(B) - 1) Wend

HEX_to_BIN = B End Function

' 用途:将二进制转化为十六进制 ' 输入:Bin(二进制数) ' 输入数据类型:String

' 输出:BIN_to_HEX(十六进制数) ' 输出数据类型:String

' 输入的最大数为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, \ End If

For i = 1 To Len(Bin) Step 4 Select Case Mid(Bin, i, 4) Case \ Case \ Case \

Case \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ End Select Next i

While Left(H, 1) = \ H = Right(H, Len(H) - 1) Wend

BIN_to_HEX = H End Function

' 用途:将十六进制转化为十进制 ' 输入:Hex(十六进制数) ' 输入数据类型:String

' 输出:HEX_to_DEC(十进制数)

' 输出数据类型:Long

' 输入的最大数为7FFFFFFF,输出的最大数为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 \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ Case \

Case \ Case \ Case \ Case \ Case \ Case \ End Select Next i

HEX_to_DEC = B End Function

' 用途:将十进制转化为十六进制 ' 输入:Dec(十进制数)

' 输入数据类型:Long

' 输出:DEC_to_HEX(十六进制数)

' 输出数据类型:String

' 输入的最大数为2147483647,输出最大数为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 \ Case \ Case \ Case \ Case \ Case \ End Select

DEC_to_HEX = a & DEC_to_HEX Dec = Dec \\ 16 Loop End Function

' 用途:将十进制转化为八进制 ' 输入:Dec(十进制数) ' 输入数据类型:Long ' 输出:DEC_to_OCT(八进制数) ' 输出数据类型:String

' 输入的最大数为2147483647,输出最大数为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

' 用途:将八进制转化为十进制 ' 输入:Oct(八进制数) ' 输入数据类型:String ' 输出:OCT_to_DEC(十进制数) ' 输出数据类型:Long

' 输入的最大数为17777777777,输出的最大数为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 \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ End Select Next i

OCT_to_DEC = B

End Function

' 用途:将二进制转化为八进制 ' 输入:Bin(二进制数) ' 输入数据类型:String ' 输出:BIN_to_OCT(八进制数) ' 输出数据类型:String

' 输入的最大数为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, \ End If

For i = 1 To Len(Bin) Step 3 Select Case Mid(Bin, i, 3)

Case \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ End Select Next i

While Left(H, 1) = \ H = Right(H, Len(H) - 1) Wend

BIN_to_OCT = H End Function

' 用途:将八进制转化为二进制 ' 输入:Oct(八进制数) ' 输入数据类型:String ' 输出:OCT_to_BIN(二进制数) ' 输出数据类型:String

' 输入的最大数为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 \ Case \ Case \ Case \ Case \ Case \ Case \ Case \ End Select Next i

While Left(B, 1) = \ B = Right(B, Len(B) - 1) Wend

OCT_to_BIN = B End Function

' 用途:将八进制转化为十六进制 ' 输入:Oct(八进制数) ' 输入数据类型:String

' 输出:OCT_to_HEX(十六进制数) ' 输出数据类型:String

' 输入的最大数为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

' 用途:将十六进制转化为八进制 ' 输入:Hex(十六进制数) ' 输入数据类型:String

' 输出:HEX_to_OCT(八进制数)

' 输出数据类型:String

' 输入的最大数为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

VB自带函数:

十进制转八进制:Oct(num)

十六进制转八进制:oct(\十进制转十六进制:hex(num) 八进制转十六进制:hex(\

十六进制转换为十进制

Dim str As String str = Text2.Text

Text10.Text = CLng(\

本文来源:https://www.bwwdw.com/article/lp45.html

Top