在.net framework中,system.convert類中提供了較為全面的各種類型、數(shù)值之間的轉(zhuǎn)換功能。其中的兩個(gè)方法可以輕松的實(shí)現(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):
可以把一個(gè)數(shù)字轉(zhuǎn)換為不同進(jìn)制數(shù)值的字符串格式,其中tobase參數(shù)為進(jìn)制的格式,只能是2、8、10及16:
如convert.tostring(2,2)執(zhí)行的結(jié)果為”0010”
現(xiàn)在我們做一個(gè)方法實(shí)現(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為原來(lái)的格式
tobase為將要轉(zhuǎn)換成的格式
新聞熱點(diǎn)
疑難解答
圖片精選