vb编程 将十进制数转换为二进制和十六进制 用do while编程
答案:2 悬赏:10 手机版
解决时间 2021-03-09 05:04
- 提问者网友:浪荡绅士
- 2021-03-08 06:59
vb编程 将十进制数转换为二进制和十六进制 用do while编程
最佳答案
- 五星知识达人网友:骨子里都是戏
- 2021-03-08 08:31
vb编程,用do-while循环实现十进制转二进制和十六进制:
1、十进制转为二进制:
Sub DecimalToBinary()
Dim intNumber As Integer
Dim intRemainder As Integer
Dim strRemainder As String
Dim strBinaryNumber As String
intNumber = CInt(InputBox("Enter number in decimal:", "DECIMAL TO BINARY CONVERSION"))
Do While intNumber >= 1
intRemainder = intNumber Mod 2
strRemainder = CStr(intRemainder)
intNumber = intNumber \ 2
strBinaryNumber = strRemainder & strBinaryNumber
Loop
MsgBox "The Number in Binary is : " & strBinaryNumber, , "DECIMAL TO BINARY CONVERSION"
End Sub2、十进制转十六进制:
Sub DecimalToHexadecimal()
Dim intNumber As Integer
Dim intRemainder As Integer
Dim strRemainder As String
Dim strHexaNumber As String
intNumber = CInt(InputBox("Enter number in decimal:", "DECIMAL TO HEXADECIMAL CONVERSION"))
Do While intNumber >= 1
intRemainder = intNumber Mod 16
strRemainder = CStr(intRemainder)
intNumber = intNumber \ 16
Select Case strRemainder
Case "10": strRemainder = "A"
Case "11": strRemainder = "B"
Case "12": strRemainder = "C"
Case "13": strRemainder = "D"
Case "14": strRemainder = "E"
Case "15": strRemainder = "F"
End Select
strHexaNumber = strRemainder & strHexaNumber
Loop
MsgBox "The Number in Hexadecimal is : " & strHexaNumber, , "DECIMAL TO HEXADECIMAL CONVERSION"
End Sub
1、十进制转为二进制:
Sub DecimalToBinary()
Dim intNumber As Integer
Dim intRemainder As Integer
Dim strRemainder As String
Dim strBinaryNumber As String
intNumber = CInt(InputBox("Enter number in decimal:", "DECIMAL TO BINARY CONVERSION"))
Do While intNumber >= 1
intRemainder = intNumber Mod 2
strRemainder = CStr(intRemainder)
intNumber = intNumber \ 2
strBinaryNumber = strRemainder & strBinaryNumber
Loop
MsgBox "The Number in Binary is : " & strBinaryNumber, , "DECIMAL TO BINARY CONVERSION"
End Sub2、十进制转十六进制:
Sub DecimalToHexadecimal()
Dim intNumber As Integer
Dim intRemainder As Integer
Dim strRemainder As String
Dim strHexaNumber As String
intNumber = CInt(InputBox("Enter number in decimal:", "DECIMAL TO HEXADECIMAL CONVERSION"))
Do While intNumber >= 1
intRemainder = intNumber Mod 16
strRemainder = CStr(intRemainder)
intNumber = intNumber \ 16
Select Case strRemainder
Case "10": strRemainder = "A"
Case "11": strRemainder = "B"
Case "12": strRemainder = "C"
Case "13": strRemainder = "D"
Case "14": strRemainder = "E"
Case "15": strRemainder = "F"
End Select
strHexaNumber = strRemainder & strHexaNumber
Loop
MsgBox "The Number in Hexadecimal is : " & strHexaNumber, , "DECIMAL TO HEXADECIMAL CONVERSION"
End Sub
全部回答
- 1楼网友:十鸦
- 2021-03-08 10:09
Dim D As Integer
Dim T As Integer
Dim B As String
Dim H As String
D = Val(InputBox("输入一个十进制整数:"))
T = D
'转换成二进制
B = ""
Do While D <> 0
B = CStr(D Mod 2) & B
D = D \ 2
Loop
Print B
'转换成十六进制
H = ""
Do While T <> 0
D = T Mod 16
Select Case D
Case 10
H = "A" & H
Case 11
H = "B" & H
Case 12
H = "C" & H
Case 13
H = "D" & H
Case 14
H = "E" & H
Case 15
H = "F" & H
Case Else
H = CStr(D) & H
End Select
T = T \ 16
Loop
Print H
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯