當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。