关于vb编程中将十进制转化为十六进制
将十进制转化为十六进制的程序中,PublicFunctionDEC_to_HEX(DecAsLong)AsStringDimaAsStringDEC_to_HEX=""D...
将十进制转化为十六进制的程序中,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能为程序做一下解释吗,每一步代表什么意思 展开
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能为程序做一下解释吗,每一步代表什么意思 展开
3个回答
推荐于2017-11-26
展开全部
Dim a As String ' 定义变量a为字符串型
DEC_to_HEX = "" ' 变量DEC_to_HEX是存放十进制转化为十六进制是结果,初值为空串 Do While Dec > 0 ' 如果要转化的十进制数Dec大于0,则进入循环
a = CStr(Dec Mod 16) ' 将Dec对16的余数转换成字符型后赋给变量a
Select Case a ' 若a为串10则重新赋值为A;若a为串11则重新赋值为B........若a为串15则重新赋值为F;
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 ' 将余数a与DEC_to_HEX连接,a在前的目的是余数倒序的原则Dec = Dec \ 16 ' 将转化的十进制数Dec除16,为下一次求余做准备,若不大于0则循环结束 Loop
DEC_to_HEX = "" ' 变量DEC_to_HEX是存放十进制转化为十六进制是结果,初值为空串 Do While Dec > 0 ' 如果要转化的十进制数Dec大于0,则进入循环
a = CStr(Dec Mod 16) ' 将Dec对16的余数转换成字符型后赋给变量a
Select Case a ' 若a为串10则重新赋值为A;若a为串11则重新赋值为B........若a为串15则重新赋值为F;
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 ' 将余数a与DEC_to_HEX连接,a在前的目的是余数倒序的原则Dec = Dec \ 16 ' 将转化的十进制数Dec除16,为下一次求余做准备,若不大于0则循环结束 Loop
展开全部
你的太麻烦了,逻辑有些复杂,这是我的代码:
Private Sub Command1_Click()
n = Val(InputBox("输入要转换的十进制整数:", "十进制转十六进制", "16"))
m = n
x = ""
Do While n >= 16
a = n Mod 16
Select Case a
Case Is < 10
a = a
x = a & x
Case Is = 10
a = "A"
x = a & x
Case Is = 11
a = "B"
x = a & x
Case Is = 12
a = "C"
x = a & x
Case Is = 13
a = "D"
x = a & x
Case Is = 14
a = "E"
x = a & x
Case Is = 15
a = "F"
x = a & x
End Select
n = n \ 16
Loop
Select Case n
Case Is < 10
n = n
Case Is = 10
n = "A"
Case Is = 11
n = "B"
Case Is = 12
n = "C"
Case Is = 13
n = "D"
Case Is = 14
n = "E"
Case Is = 15
n = "F"
End Select
b = x
x = n & x
MsgBox m & "换成十六进制数字是:" & x & " " & Chr(10) & "a为:" & a & " " & Chr(10) & "n为:" & n & " " & Chr(10) & "x为:" & b '输出a,n,b是为了检测方便
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-23
展开全部
直接用HEX()函数不就可以了,还那么麻烦
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询