日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

C#實現(xiàn)字符串進制轉(zhuǎn)換方法匯總_C#教程

作者:ou.cs ? 更新時間: 2022-12-13 編程語言

C# 字符串進制轉(zhuǎn)換

        /// <summary>
        /// 進制轉(zhuǎn)換
        /// </summary>
        /// <param name="input"></param>
        /// <param name="fromType">原來的進制格式</param>
        /// <param name="toType">要轉(zhuǎn)換成的進制格式</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>
        /// 從二進制轉(zhuǎn)換成其他進制
        /// </summary>
        /// <param name="input"></param>
        /// <param name="toType"></param>
        /// <returns></returns>
        private string ConvertGenericBinaryFromBinary(string input, byte toType)
        {
            switch (toType)
            {
                case 8:
                    //先轉(zhuǎn)換成十進制然后轉(zhuǎn)八進制
                    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>
        /// 從八進制轉(zhuǎn)換成其他進制
        /// </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>
        /// 從十進制轉(zhuǎn)換成其他進制
        /// </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>
        /// 從十六進制轉(zhuǎn)換成其他進制
        /// </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#進制轉(zhuǎn)換方法匯總

進制轉(zhuǎn)換匯總

1.十進制數(shù)轉(zhuǎn)二進制數(shù)(結(jié)果:11)

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

2.十進制數(shù)轉(zhuǎn)二進制數(shù)(結(jié)果:0011) ?(左側(cè)補位方法,轉(zhuǎn)八進制,轉(zhuǎn)十六進制……與之類似)

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

3.十進制數(shù)轉(zhuǎn)二進制數(shù)(結(jié)果:1100) ?(右側(cè)補位方法,轉(zhuǎn)八進制,轉(zhuǎn)十六進制……與之類似)

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

4.十六進制轉(zhuǎn)二進制數(shù)(結(jié)果:00001111)

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

5.十六進制轉(zhuǎn)十進制數(shù)

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

6.二進制轉(zhuǎn)十進制(結(jié)果:15)

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

7.十六進制轉(zhuǎn)十進制(結(jié)果:48)

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

8.十進制轉(zhuǎn)十六進制(結(jié)果:f)

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

9.二進制轉(zhuǎn)十六進制(結(jié)果:f)

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

10.字符轉(zhuǎn)十進制數(shù)(結(jié)果:65)

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

11.將字符串中第1個字符轉(zhuǎn)成十進制數(shù)(結(jié)果:65)? ? ? ? ?

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

? ? ? ? ? ? Console.WriteLine(s16);      

12.十六進制轉(zhuǎn)浮點數(shù)(結(jié)果:0.68)

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

13,浮點數(shù)轉(zhuǎn)十六進制數(shù)

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

14.int 轉(zhuǎn)字符串

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

15.字符串轉(zhuǎn)int

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

16.byte 轉(zhuǎn) int?

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

17.ushort[]轉(zhuǎn)換為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轉(zhuǎn)換為ushort[]類型?? ? ? ? ?

 ?float value;

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

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

原文鏈接:https://blog.csdn.net/weixin_44291381/article/details/127841947

欄目分類
最近更新