[C#][VB.Net]整數與十六進制字串互轉

摘要:整數與十六進制字串互轉

整數轉成十六進制字串

C#

        private string IntToHexString(int value)
        {
            return "0x" + String.Format("{0:X}", value);
        }

VB.Net

        Private Function IntToHexString(ByVal value As Integer) As string
            Return "0x" & String.Format("{0:X}", value)
        End Function


十六進制字串轉成整數

C#

        private int HexStringToInt(string value)
        {
            if (value.ToUpper().StartsWith("0X"))
                value = value.Substring(2);
            return Int32.Parse(value, System.Globalization.NumberStyles.HexNumber );
        }

VB.Net

    Private Function HexStringToInt(ByVal value As String) As Integer
        If value.ToUpper().StartsWith("0X") Then
            value = value.Substring(2)
        End If
        Return Int32.Parse(value, System.Globalization.NumberStyles.HexNumber)
    End Function