由于二進(jìn)制數(shù)在C#中無(wú)法直接表示蹲诀,所以所有二進(jìn)制數(shù)都用一個(gè)字符串來(lái)表示
例如: 二進(jìn)制: 1010 表示為 字符串:"1010"
int d = 10;
//十進(jìn)制轉(zhuǎn)二進(jìn)制字符串
Console.WriteLine(Convert.ToString(d,2));
//輸出: 1010
//十進(jìn)制轉(zhuǎn)十六進(jìn)制字符串
Console.WriteLine(Convert.ToString(d,16));
//輸出: a
//二進(jìn)制字符串轉(zhuǎn)十進(jìn)制數(shù)
string bin = "1010";
Console.WriteLine(Convert.ToInt32(bin,2));
//輸出: 10
//二進(jìn)制字符串轉(zhuǎn)十六進(jìn)制數(shù)
string bin = "1010";
Console.WriteLine(string.Format("{0:x}",Convert.ToInt32(bin,2));
//輸出: a
//十六進(jìn)制轉(zhuǎn)二進(jìn)制字符串
Console.WriteLine(Convert.ToString(0xa,2));
//輸出: 1010
//十六進(jìn)制轉(zhuǎn)十進(jìn)制數(shù)
Console.WriteLine(Convert.ToString(0xa,10));
//輸出: 10