fjfjfjfjfjfj

【.Net】2、8、16进制转换


    Private Function Asc2String(ByVal str As String) As String
        Dim StrDesc As System.String = String.Empty
        If str = String.Empty OrElse str.Length Mod 2 <> 0 Then
            MessageBox.Show("Input string error.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Asc2String = String.Empty
            Exit Function
        End If
        For i As System.Int32 = 0 To str.Length() - 1 Step 2
            Dim s As System.String = Mid(str, i + 1, 2)
            Dim num As System.Int32 = Convert.ToInt32(s, 16)
            If num < 0 OrElse num > 256 Then
                MessageBox.Show(String.Format("Input number {0} error.", num.ToString()), "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Asc2String = String.Empty
                Exit Function
            End If
            StrDesc = StrDesc & ChrW(num)
        Next
        Asc2String = StrDesc.Trim
    End Function

 

Public Shared Function ToInt32(ByVal value As String, ByVal fromBase As Integer) As Integer

成员属于: System.Convert
摘要:
将指定基数的数字的 System.String 表示形式转换为等效的 32 位有符号整数。

参数:
value: 包含数字的 System.String。
fromBase: value 中数字的基数,它必须是 2、8、10 或 16。

返回值:
等效于 value 中的数字的 32 位有符号整数。 - 或 - 如果 value 为 null,则为零。

异常:
System.ArgumentException: fromBase 不是 2、8、10 或 16。 - 或 - value,它表示一个非 10 为基的有符号数,前面带一个负号。
System.FormatException: value 包含的一个字符不是 fromBase 指定的基中的有效数字。如果 value 中的第一个字符无效,异常消息则指示没有可转换的数字;否则,该消息将指示 value 包含无效的尾随字符。
System.OverflowException: value,它表示一个非 10 为基的有符号数,前面带一个负号。 - 或 - 返回值小于 System.Int32.MinValue 或大于 System.Int32.MaxValue。

 

 

    Private Function String2Asc(ByVal str As String) As String
        Dim StrDesc As System.String = String.Empty
        For i As System.Int32 = 0 To str.Length() - 1
            Dim s As System.Char = str(i)
            StrDesc = StrDesc & Convert.ToString(AscW(s), 16)
        Next
    End Function

Public Shared Function ToString(ByVal value As Integer, ByVal toBase As Integer) As String
成员属于: System.Convert
摘要:
将 32 位有符号整数的值以指定的基数转换为它的等效 System.String 表示形式。

参数:
value: 32 位有符号整数。
toBase: 返回值的基数,必须是 2、8、10 或 16。

返回值:
以 toBase 为基数的 value 的 System.String 表示形式。

异常:
System.ArgumentException: toBase 不是 2、8、10 或 16。

 

posted on 2013-09-23 20:37  撬棍  阅读(313)  评论(0编辑  收藏  举报

导航