zoukankan      html  css  js  c++  java
  • 进制数换算

    '十进制数转换成十六进制数
        Public Shared Function DecToHex(ByVal value As Integer) As String
            Return BinToHex(DecToBin(value))
        End Function

        '二进制数转换成十进制数
        Public Shared Function BinToDec(ByVal value As String) As Integer
            Dim i As Integer
            Dim returnValue As Integer = 0
            For i = value.Length - 1 To 0 Step -1
                returnValue += Val(value.Chars(value.Length - 1 - i)) * (2 ^ i)
            Next

            Return returnValue
        End Function

        '任意二进制数转换成十六进制数
        Public Shared Function BinToHex(ByVal value As String) As String
            If value.Length Mod 4 <> 0 Then
                value = value.PadLeft(4 * (value.Length / 4 + 1), "0"c)
            End If

            Dim i As Integer
            Dim returnString As String = ""
            Dim tempString As String
            For i = 0 To value.Length / 4 - 1
                tempString = value.Chars(4 * i) + value.Chars(4 * i + 1) _
                         + value.Chars(4 * i + 2) + value.Chars(4 * i + 3)

                returnString += GetHexChar(tempString)
            Next

            Return returnString
        End Function

        '特殊情况:4位二进制数转换成十六进制数
        Public Shared Function GetHexChar(ByVal value As String) As String
            Dim tempValue As Integer
            tempValue = BinToDec(value)

            If tempValue < 10 Then
                Return CStr(tempValue)
            Else
                Select Case tempValue - 10
                    Case 0
                        Return "A"
                    Case 1
                        Return "B"
                    Case 2
                        Return "C"
                    Case 3
                        Return "D"
                    Case 4
                        Return "E"
                    Case 5
                        Return "F"
                End Select
            End If
        End Function

        '仅限非负整数
        '十进制数转换成二进制数
        Public Shared Function DecToBin(ByVal value As Integer) As String
            If value = 0 Then Return "0"

            Dim tempValue As Integer = value
            Dim tempString As String = ""

            Dim modValue As Integer
            Do
                tempString += CStr(tempValue Mod 2)
                tempValue = tempValue / 2
            Loop While (tempValue <> 0)

            Dim i As Integer
            Dim returnChars As Char() = tempString.ToCharArray()
            Dim returnString As String = ""
            For i = tempString.Length - 1 To 0 Step -1
                returnString += returnChars(i)
            Next

            Return returnString
        End Function

        '十六进制数转换成十进制数
        Public Shared Function HexToDec(ByVal value As String) As Integer
            Dim i As Integer
            Dim returnValue As Integer = 0
            For i = value.Length - 1 To 0 Step -1
                Dim num As Integer = 0
                Select Case value.Chars(value.Length - 1 - i)
                    Case "0"c To "9"c
                        num = Val(value.Chars(i))
                    Case "A"c
                        num = 10
                    Case "B"c
                        num = 11
                    Case "C"c
                        num = 12
                    Case "D"c
                        num = 13
                    Case "E"c
                        num = 14
                    Case "F"c
                        num = 15
                End Select

                returnValue += num * (16 ^ i)
            Next

            Return returnValue
        End Function

  • 相关阅读:
    (转)R空间数据处理与可视化
    shell 脚本 exit 1 报错:numeric argument required问题解决
    如何在java中拟合正态分布
    Geometry关系高级操作
    IntelliJ IDEA 15.0.2远程debug tomcat
    jmc远程监控java服务
    Cannot assign requested address出现的原因及解决方案
    网络调试工具
    性能优化工具---sar
    性能优化工具---iostat
  • 原文地址:https://www.cnblogs.com/secbook/p/2655365.html
Copyright © 2011-2022 走看看