其中的兩個(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)制的字符串:
復(fù)制代碼代碼如下:
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)換成的格式