亲宝软件园·资讯

展开

C#实现字符串进制转换方法汇总

ou.cs 人气:0

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);

加载全部内容

相关教程
猜你喜欢
用户评论