计算机等级考试二级VB常用算法(4):进制转化

全国等级考试资料网 2022-07-19 20:02:43 159

1、算法说明

1)        十进制正整数m转换为R(2-16)进制的字符串。
         思路: 将m不断除r取余数,直到商为0,将余数反序即得到结果。
         算法实现:
Private Function Tran(ByVal m As Integer, ByVal r As Integer) As String
    Dim StrDtoR As String, n As Integer
    Do While m <> o
        n = m Mod r
        m = m r
        If n > 9 Then
            StrDtoR = Chr(65 + n - 10) & StrDtoR
        Else
            StrDtoR = n & StrDtoR
        End If
    Loop
    Tran = StrDtoR
End Function  
2)        R(2-16)进制字符串转换为十进制正整数。
         思路:R进制数每位数字乘以权值之和即为十进制数。
         算法实现:
Private Function Tran(ByVal s As String, ByVal r As Integer) As integer
    Dim n As Integer, dec As Integer
    s = UCase(Trim(s))
    For i% = 1 To Len(s)
               If Mid(s, i, 1) >= "A" Then
                        n = Asc(Mid(s, i, 1)) - Asc("A") + 10
               Else
                        n = Val(Mid(s, i, 1))
               End If
               dec = dec + n * r ^ (Len(s) - i)
    Next i
    Tran = dec
End Function  首页 1 2 尾页 相关资料

相关阅读