添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

標籤:c#

有兩種方法:
1. 要轉換成有符號整數,使用 Convert.ToInt32(string, 16) ;
要轉換成不帶正負號的整數,使用 Convert.ToUInt32(string, 16) 。
如果 16 進位的字串不是以 0x 或 0X 開頭,比如“af37”,用這種方法也能進行轉換。

2. 要轉換成有符號整數,使用 new System.ComponentModel.Int32Converter().ConvertFromString(string) ;
要轉換成不帶正負號的整數,使用 new System.ComponentModel.UInt32Converter().ConvertFromString(string) 。
注意:
使用這種方法要進行強制類型轉換。
如果 16 進位的字串不是以 0x 或 0X 開頭,不能用這種方法。

這兩種方法對字母的大小寫都不敏感,字串以0X或0x開頭都可以。對於16進位表示中可能出現的字元( 從a 到 f),用大寫或小寫表示都行,甚至是大小寫混雜的表示形式也可以,比如像 0Xa3Bf2 這樣的字串能夠被正確處理。
不管使用哪一種方法,都不要忘記要對可能拋出的異常進行處理。

下面給出一段範例程式碼:

/************************************************** * Author: HAN Wei * Author's blog: http://blog.csdn.net/henter/ * Date: April 22nd, 2015 * Description: demonstrate how to convert hexadecimal string*              with leading 0x into integer**************************************************/using System;namespace HexStringConvertToInt{    class Program    {        static void Main(string[] args)        {            string hexValue = "0Xa3Bf2b10";            // Method 1:            Console.WriteLine("Method 1:");            int decValue = 0;            try            {                decValue = Convert.ToInt32(hexValue, 16);            }            catch (Exception e)            {                Console.WriteLine("An error occurred while converting!");                Console.WriteLine("Error message: " + e.Message);                Console.ReadLine();                return;            }            Console.WriteLine("signed integer value: {0}", decValue);                        uint uDecValue = 0;            try            {                uDecValue = Convert.ToUInt32(hexValue, 16);            }            catch (Exception e)            {                Console.WriteLine("An error occurred while converting!");                Console.WriteLine("Error message: " + e.Message);                Console.ReadLine();                return;            }            Console.WriteLine("unsigned integer value: {0}", uDecValue);            Console.WriteLine();            // Method 2:            Console.WriteLine("------------------------------");            Console.WriteLine("Method 2:");            int iValue = 0;            try            {                iValue = (int)new System.ComponentModel.Int32Converter().ConvertFromString(hexValue);            }            catch (Exception e)            {                Console.WriteLine("An error occurred while converting!");                Console.WriteLine("Error message: " + e.Message);                Console.ReadLine();                return;            }            Console.WriteLine("signed integer value: {0}", iValue);                        uint uValue = 0;            try            {                uValue = (uint)new System.ComponentModel.UInt32Converter().ConvertFromString(hexValue);            }            catch (Exception e)            {                Console.WriteLine("An error occurred while converting!");                Console.WriteLine("Error message: " + e.Message);                Console.ReadLine();                return;            }            Console.WriteLine("unsigned integer value: {0}", uValue);            Console.ReadLine();        }    }}

C#中將以0x開頭的16進位字串轉換成對應的整數

本文章原先以中文撰寫並發佈於 aliyun.com,亦設英文版本,僅作資訊用途。本網站不對文章的準確性,完整性或可靠性或其任何翻譯作出任何明示或暗示的陳述或保證。如對該文章有任何疑慮或投訴,請傳送電郵至 info-contact@alibabacloud.com 並提供相關疑慮或投訴的詳細說明。職員會於 5 個工作天內與您聯絡,一經驗證之後,即會刪除該侵權內容。

相關關鍵詞: 0x blockchain instruction 0x referenced memory 0x color codes hex notation 0x windows error codes 0x 0x hex converter 16 x 16 tile C# 定時執行一個方法 c語言實現排列組合演算法問題 各大微博短網址(ShortUrl)的演算法 C# C#操作INI檔案(調用WindowsAPI函數:WritePrivateProfileString,GetPrivate... C#全形和半形轉換

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

Get Started for Free //intl.aliyun.com/pricing //intl.aliyun.com/trust-center //intl.aliyun.com/free-trial/enterprise //intl.aliyun.com/startup //intl.aliyun.com/partner