asp.net(c#)下各種進(jìn)制間的輕松轉(zhuǎn)換(2進(jìn)制、8進(jìn)制、10進(jìn)制、16進(jìn)制)
更新時間:2010年10月20日 18:07:54 作者:
在.NET Framework中,System.Convert類中提供了較為全面的各種類型、數(shù)值之間的轉(zhuǎn)換功能。
其中的兩個方法可以輕松的實現(xiàn)各種進(jìn)制的數(shù)值間的轉(zhuǎn)換:
Convert.ToInt32(string value, int fromBase):
可以把不同進(jìn)制數(shù)值的字符串轉(zhuǎn)換為數(shù)字,其中fromBase參數(shù)為進(jìn)制的格式,只能是2、8、10及16:
如Convert.ToInt32(”0010”,2)執(zhí)行的結(jié)果為2;
Convert.ToString(int value, int toBase):
可以把一個數(shù)字轉(zhuǎn)換為不同進(jìn)制數(shù)值的字符串格式,其中toBase參數(shù)為進(jìn)制的格式,只能是2、8、10及16:
如Convert.ToString(2,2)執(zhí)行的結(jié)果為”0010”
現(xiàn)在我們做一個方法實現(xiàn)各種進(jìn)制間的字符串自由轉(zhuǎn)換:選把它轉(zhuǎn)成數(shù)值型,然后再轉(zhuǎn)成相應(yīng)的進(jìn)制的字符串:
public string ConvertString(string value, int fromBase, int toBase)
{
int intValue = Convert.ToInt32(value, fromBase);
return Convert.ToString(intValue, toBase);
}
其中fromBase為原來的格式
toBase為將要轉(zhuǎn)換成的格式
Convert.ToInt32(string value, int fromBase):
可以把不同進(jìn)制數(shù)值的字符串轉(zhuǎn)換為數(shù)字,其中fromBase參數(shù)為進(jìn)制的格式,只能是2、8、10及16:
如Convert.ToInt32(”0010”,2)執(zhí)行的結(jié)果為2;
Convert.ToString(int value, int toBase):
可以把一個數(shù)字轉(zhuǎn)換為不同進(jìn)制數(shù)值的字符串格式,其中toBase參數(shù)為進(jìn)制的格式,只能是2、8、10及16:
如Convert.ToString(2,2)執(zhí)行的結(jié)果為”0010”
現(xiàn)在我們做一個方法實現(xiàn)各種進(jìn)制間的字符串自由轉(zhuǎn)換:選把它轉(zhuǎn)成數(shù)值型,然后再轉(zhuǎn)成相應(yīng)的進(jìn)制的字符串:
復(fù)制代碼 代碼如下:
public string ConvertString(string value, int fromBase, int toBase)
{
int intValue = Convert.ToInt32(value, fromBase);
return Convert.ToString(intValue, toBase);
}
其中fromBase為原來的格式
toBase為將要轉(zhuǎn)換成的格式
相關(guān)文章
基于.net standard 的動態(tài)編譯實現(xiàn)代碼
這篇文章主要介紹了基于.net standard 的動態(tài)編譯實現(xiàn)代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07c# 讀取Northwind數(shù)據(jù)庫image字段
我在寫一個三層結(jié)構(gòu)Demo時,使用了Northwind這個范例數(shù)據(jù)庫。但是奇怪的是,讀取Categories表的Picture列(image類型)無法在image控件中正常顯示(解決方案在后面代碼中可以看到)。2009-03-03.Net中導(dǎo)出數(shù)據(jù)到Excel(asp.net和winform程序中)
.Net中導(dǎo)出數(shù)據(jù)到Excel包括以下兩種情況:asp.net中導(dǎo)出Excel的方法/winForm中導(dǎo)出Excel的方法,針對以上兩種情況做了下詳細(xì)的實現(xiàn)代碼,感興趣的朋友可不要錯過了哈,希望本文對你有所幫助2013-02-02asp.net 不用GridView自帶刪除功能,刪除一行數(shù)據(jù)
數(shù)據(jù)表一定要有個ID的主鍵值,你的gridview要設(shè)定一下DataKeyNames="ID"這個屬性值,接下的事件就好多了,寫個OnRowDeleting事件就可以了。2009-11-11ASP.NET Global.asax應(yīng)用程序文件簡介
Global.asax 文件,有時候叫做 ASP.NET 應(yīng)用程序文件,提供了一種在一個中心位置響應(yīng)應(yīng)用程序級或模塊級事件的方法。2009-03-03