当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Uri.FromHex()用法及代码示例


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

用法: public static int FromHex (char digit);
Here, it takes the hexadecimal digit (0-9, a-f, A-F) to convert.

返回值:此方法返回一个Int32值,该值包含一个从0到15的数字,该数字对应于指定的十六进制数字。


异常:如果该数字不是有效的十六进制数字(0-9,a-f,A-F),则此方法将引发ArgumentException。

以下示例程序旨在说明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

参考:



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 C# | Uri.FromHex() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。