首頁 > 軟體

C#實現字串進位制轉換方法彙總

2022-11-15 14:00:47

C# 字串進位制轉換

        /// <summary>
        /// 進位制轉換
        /// </summary>
        /// <param name="input"></param>
        /// <param name="fromType">原來的進位制格式</param>
        /// <param name="toType">要轉換成的進位制格式</param>
        /// <returns></returns>
        public string ConvertGenericBinary(string input, byte fromType, byte toType)
        {
            string output = input;
            switch (fromType)
            {
                case 2:
                    output = ConvertGenericBinaryFromBinary(input, toType);
                    break;
                case 8:
                    output = ConvertGenericBinaryFromOctal(input, toType);
                    break;
                case 10:
                    output = ConvertGenericBinaryFromDecimal(input, toType);
                    break;
                case 16:
                    output = ConvertGenericBinaryFromHexadecimal(input, toType);
                    break;
                default:
                    break;
            }
            return output;
        }

        /// <summary>
        /// 從二進位制轉換成其他進位制
        /// </summary>
        /// <param name="input"></param>
        /// <param name="toType"></param>
        /// <returns></returns>
        private string ConvertGenericBinaryFromBinary(string input, byte toType)
        {
            switch (toType)
            {
                case 8:
                    //先轉換成十進位制然後轉八進位制
                    input = Convert.ToString(Convert.ToInt32(input, 2), 8);
                    break;
                case 10:
                    input = Convert.ToInt32(input, 2).ToString();
                    break;
                case 16:
                    input = Convert.ToString(Convert.ToInt32(input, 2), 16);
                    break;
                default:
                    break;
            }
            return input;
        }

        /// <summary>
        /// 從八進位制轉換成其他進位制
        /// </summary>
        /// <param name="input"></param>
        /// <param name="toType"></param>
        /// <returns></returns>
        private string ConvertGenericBinaryFromOctal(string input, byte toType)
        {
            switch (toType)
            {
                case 2:
                    input = Convert.ToString(Convert.ToInt32(input, 8), 2);
                    break;
                case 10:
                    input = Convert.ToInt32(input, 8).ToString();
                    break;
                case 16:
                    input = Convert.ToString(Convert.ToInt32(input, 8), 16);
                    break;
                default:
                    break;
            }
            return input;
        }

        /// <summary>
        /// 從十進位制轉換成其他進位制
        /// </summary>
        /// <param name="input"></param>
        /// <param name="toType"></param>
        /// <returns></returns>
        private string ConvertGenericBinaryFromDecimal(string input, int toType)
        {
            string output = "";
            int intInput = Convert.ToInt32(input);
            switch (toType)
            {
                case 2:
                    output = Convert.ToString(intInput, 2);
                    break;
                case 8:
                    output = Convert.ToString(intInput, 8);
                    break;
                case 16:
                    output = Convert.ToString(intInput, 16);
                    break;
                default:
                    output = input;
                    break;
            }
            return output;
        }

        /// <summary>
        /// 從十六進位制轉換成其他進位制
        /// </summary>
        /// <param name="input"></param>
        /// <param name="toType"></param>
        /// <returns></returns>
        private string ConvertGenericBinaryFromHexadecimal(string input, int toType)
        {
            switch (toType)
            {
                case 2:
                    input = Convert.ToString(Convert.ToInt32(input, 16), 2);
                    break;
                case 8:
                    input = Convert.ToString(Convert.ToInt32(input, 16), 8);
                    break;
                case 10:
                    input = Convert.ToInt32(input, 16).ToString();
                    break;
                default:
                    break;
            }
            return input;
        }

C#進位制轉換方法彙總

進位制轉換匯總

1.十進位制數轉二進位制數(結果:11)

 string s1 = Convert.ToString(3, 2);

2.十進位制數轉二進位制數(結果:0011)  (左側補位方法,轉八進位制,轉十六進位制……與之類似)

 string s2 = Convert.ToString(3, 2).PadLeft(4, '0');          

3.十進位制數轉二進位制數(結果:1100)  (右側補位方法,轉八進位制,轉十六進位制……與之類似)

  string s3 = Convert.ToString(3, 2).PadRight(4, '0');

4.十六進位制轉二進位制數(結果:00001111)

 string s4 = Convert.ToString(0xf, 2).PadLeft(8, '0');
            string s5 = Convert.ToString(0xF, 2).PadLeft(8, '0');  //不區分大小寫,結果一樣。           

5.十六進位制轉十進位制數

   string s6 = Convert.ToString(0xf, 10);                  //結果:15,string型別
            string s7 = Convert.ToString(0xf, 10).PadLeft(4, '0');  //結果:0015,string型別
            int s8 = Convert.ToInt32("f", 16);                     //結果:15,int型別        

6.二進位制轉十進位制(結果:15)

  int s9 = Convert.ToInt32("1111", 2);          

7.十六進位制轉十進位制(結果:48)

  int s10 = Convert.ToInt32("30", 16);         

8.十進位制轉十六進位制(結果:f)

 string s12 = string.Format("{0:x0}", 15);
            // 第2種方法:
            string s12 = convert.ToString(15,16);            

9.二進位制轉十六進位制(結果:f)

  string s13 = string.Format("{0:x}", Convert.ToInt32("1111", 2));         

10.字元轉十進位制數(結果:65)

    int s14 = Convert.ToInt32('A');        

11.將字串中第1個字元轉成十進位制數(結果:65)         

     string s15 = "AB";
            int s16 = Convert.ToInt32(Convert.ToChar(s15[0]));

            Console.WriteLine(s16);      

12.十六進位制轉浮點數(結果:0.68)

  byte[] bt = new byte[4] { 0x7b, 0x14, 0x2e, 0x3f };
            float ff = BitConverter.ToSingle(bt, 0);         

13,浮點數轉十六進位制數

  float f = 0.68f;
            string str1 = BitConverter.ToString(BitConverter.GetBytes(f)).Replace("-", ""); //結果:7B142E3F
            string str2 = BitConverter.ToString(BitConverter.GetBytes(f));    //結果:7B-14-2E-3F          

14.int 轉字串

  int n = 49;
            string str1 = n.ToString();     //結果:49
            string str2 = n.ToString("x");  //結果:0x31         

15.字串轉int

    string str = "31";
            int i = Convert.ToInt32(str);  //結果:31       

16.byte 轉 int 

 byte bt = 0x31;
            int i = Convert.ToInt32(bt);  //結果;49           

17.ushort[]轉換為float型別           

 ushort[] val ;      

            List<byte> result = new List<byte>();

           result.AddRange(BitConverter.GetBytes(val[0]));

           result.AddRange(BitConverter.GetBytes(val[1]));

           float JG= BitConverter.ToSingle(result.ToArray(), 0);

18.float轉換為ushort[]型別          

  float value;

           ushort lowOrderValue = BitConverter.ToUInt16(BitConverter.GetBytes(value), 2);

           ushort highOrderValue = BitConverter.ToUInt16(BitConverter.GetBytes(value), 0);

到此這篇關於C#實現字串進位制轉換的文章就介紹到這了,更多相關C# 字串進位制轉換內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com