[.NET]數值各進制間的轉換

[.NET]數值各進制間的轉換

MSDN論壇 中有看到一篇 進位轉換問題 參考 asp.net(c#)下各種進位間的輕松轉換(2進位、8進位、10進) 這篇中,使用它包的 ConvertString Method就可以自由轉來轉去!

另外,也可依每個人的需求再加以包裝,如筆者是將這個Method包裝成Extension Methods,如下,

{
    Binary = 2,
    Octal = 8,
    Decimal = 10,
    Hex = 16
}

public static class MyExtension
{

     #region "數字轉成2, 8, 16進位的字串"

        public static string ToBinary(this int value)
        {
            return ConvertString(value.ToString(), BaseFormat.Decimal, BaseFormat.Binary);
        }

        public static string ToOctal(this int value)
        {
            return ConvertString(value.ToString(), BaseFormat.Decimal, BaseFormat.Octal);
        }

        public static string ToHex(this int value)
        {
            return ConvertString(value.ToString(), BaseFormat.Decimal, BaseFormat.Hex);
        }

    #endregion 


    #region "字串2, 8, 16進位的字串轉成10進位的數字"

        public static int FromBinaryToInt(this string value)
        {
            string result = ConvertString(value.ToString(), BaseFormat.Binary , BaseFormat.Decimal);
            int myResult = 0;
            int.TryParse(result, out myResult);
            return myResult;
        }

        public static int FromOctalToInt(this string value)
        {
            string result = ConvertString(value.ToString(), BaseFormat.Octal, BaseFormat.Decimal);
            int myResult = 0;
            int.TryParse(result, out myResult);
            return myResult;
        }

        public static int FromHexToInt(this string value)
        {
            string result = ConvertString(value.ToString(), BaseFormat.Hex, BaseFormat.Decimal);
            int myResult = 0;
            int.TryParse(result, out myResult);
            return myResult;
        }

    #endregion

    public static string ConvertString(this string value, BaseFormat fromBase, BaseFormat toBase)
    {
        int intValue = Convert.ToInt32(value, (int)fromBase);
        return Convert.ToString(intValue, (int)toBase);
    } 
}

使用的方式如下,

int intValue = 13;
Console.WriteLine(string.Format("int[{0}] to Binary[{1}]", intValue.ToString(), intValue.ToBinary()));
Console.WriteLine(string.Format("int[{0}] to Octal[{1}]", intValue.ToString(), intValue.ToOctal()));
Console.WriteLine(string.Format("int[{0}] to Hex[{1}]", intValue.ToString(), intValue.ToHex()));

//2, 8, 16進制2 int
Console.WriteLine(string.Format("string Binary[{0}] to int[{1}]", "1101", "1101".FromBinaryToInt()));
Console.WriteLine(string.Format("string Octal[{0}] to int[{1}]", "15", "15".FromOctalToInt()));
Console.WriteLine(string.Format("string Hex[{0}] to int[{1}]", "d", "d".FromHexToInt()));

//字串轉轉樂
//2 to 16
Console.WriteLine(string.Format("string Binary[{0}] to Hex[{1}]", "1101"
, "1101".ConvertString( BaseFormat.Binary,   BaseFormat.Hex )));
//16 to 8
Console.WriteLine(string.Format("string Hex[{0}] to Octal[{1}]", "d"
, "d".ConvertString(BaseFormat.Hex, BaseFormat.Octal)));

輸出的結果如下,

int[13] to Octal[15]
int[13] to Hex[d]
string Binary[1101] to int[13]
string Octal[15] to int[13]
string Hex[d] to int[13]
string Binary[1101] to Hex[d]
string Hex[d] to Octal[15]

 

範例程式:

Hi, 

亂馬客Blog已移到了 「亂馬客​ : Re:從零開始的軟體開發生活

請大家繼續支持 ^_^