📜  C#| Uri.FromHex()方法

📅  最后修改于: 2021-05-29 20:30:19             🧑  作者: Mango

Uri.FromHex(Char)方法用于获取十六进制数字的十进制值。

下面的程序说明了Uri.FromHex(Char)方法的用法:

范例1:

// C# program to demonstrate the
// Uri.FromHex(Char) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing Char value
        char value = 'A';
  
        // Gets the decimal value 
        // of a hexadecimal digit.
        // using FromHex() method
        int dec = Uri.FromHex(value);
  
        // Displaying the result
        Console.WriteLine("Converted int value : {0}", dec);
    }
}
输出:
Converted int value : 10

示例2:对于ArgumentException

// C# program to demonstrate the
// Uri.FromHex(Char) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring and initializing
            // Char value
            char value = '.';
  
            // Gets the decimal value 
            // of a hexadecimal digit.
            // using FromHex() method
            int dec = Uri.FromHex(value);
  
            // Displaying the result
            Console.WriteLine("Converted int value : {0}", dec);
        }
  
        catch (ArgumentException e) 
        {
            Console.WriteLine("Digit should be a valid "+
                   "Hexadecimal digit (0-9, a-f, A-F).");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
Digit should be a valid Hexadecimal digit (0-9, a-f, A-F).
Exception Thrown: System.ArgumentException

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.uri.fromhex?view=netstandard-2.1