本文介绍了如何在c#中将带二进制数的字符串列表转换为十进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用过这个

1 --------

temp = Convert.ToString(i,10);



2 -----

decimal.TryParse(i,out n);



结果是零



代码:

I used this
1--------
temp = Convert.ToString(i, 10);

2-----
decimal.TryParse(i, out n);

result are zero

code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GenaticAlgo
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        int counter,decimal_val = 0;
       // string i;
        List<string> generatedNum = new List<string>();
        List<string> EvalValue = new List<string>();

        int GetSL(int final, int start, int pre)
        {
            int size, length;
            length = final - start;
            size = length * pre;
            while (size > 1)
            {
                size = size / 2;
                counter++;
            }
            return counter;
        }

        string temp;
        
        List<string> Generate(int NumOfPopSize, int LenChromosom)
        {
            Random ran = new Random();
            List<string> listOfRandom = new List<string>();
            for (int i = 0; i < NumOfPopSize; i++)
            {
                for (int j = 0; j < LenChromosom; j++)
                {
                    temp += ran.Next(0, 2);
                }
                listOfRandom.Add(temp);
                temp = " ";
            }
            return listOfRandom;
        }

        List<string> Eval(List<string> ListBinary)
        {
            List<int> ListInt = new List<int>();
          List<string>ListDec=new List<string>();
        //  string temp = "";
          decimal n ;

          foreach (string i in ListBinary)
          {
              ListInt=Convert.ToInt64(i)

          }
          /* for (decimal i = 0; i < ListBinary.Count; i++)
       {
           n =Convert.ToDecimal(i, 10);
           ListDec.Add(n.ToString());
       }*/

          /*   // return 0
             foreach (string i in ListBinary)
             {
                 decimal.TryParse(i, out n);
               
                 ListDec.Add(n.ToString());
                
             } */
          /* for (int i = 0; i <ListBinary.Count; i++)
          {
            //  temp = Convert.ToString(i, 10);
            // temp = Convert.ToInt64(i, 10);
            
                
              ListDec.Add(temp);
          }*/

          //foreach (i in ListBinary)
          //{
          //   temp = Convert.ToString(i, 10);
          //    ListDec.Add(temp);
          //}

          // temp = "";
          /*   int  base_val = 1, rem;
           foreach (string i in ListBinary)
           {
               temp = i;
               int num = Convert.ToInt32(temp);
               while (num > 0)
               {
                   rem = num % 10;
                   decimal_val = decimal_val + rem * base_val;
                   num = num / 10;
                   base_val = base_val * 2;
               }
               ListDec.Add((decimal_val).ToString());
           }*/

              return ListDec;

        }
        private void btnShow_Click(object sender, EventArgs e)
        {
           

            int Start = int.Parse(textbStart.Text);
            int Final = int.Parse(textbFinal.Text);
            int precesion = int.Parse(textbPrecision.Text);
            int PopSize = int.Parse(textbPopulationSize.Text);
            int numOfGenerate = GetSL(Final, Start,precesion);

            generatedNum = Generate(numOfGenerate, PopSize);
            listBoxRandom.DataSource = generatedNum;
           
        }

        private void btnEvaluation_Click(object sender, EventArgs e)
        {
            EvalValue = Eval(generatedNum);
            listBoxFitness.DataSource = EvalValue;
        }
    }
}




推荐答案

static void Main(string[] args)
        {
            List<string> listBinary = new List<string>();
            listBinary.Add("101");
            listBinary.Add("111");
            listBinary.Add("110");
            listBinary.Add("1111");
            List<string> listDecimal = new List<string>();

            foreach (var str in listBinary)
            {
                listDecimal.Add(ConvertBinaryToDecimal(str));
            }
            foreach (var str in listDecimal)
            {
                Console.WriteLine(str);
            }

        }

        private static string ConvertBinaryToDecimal(string a)
        {
            double str = 0;
            char[] arr = a.ToCharArray();
            int i = arr.Length - 1;
            foreach (char c in arr)
            {
                str = str + Math.Pow(2, i)*Convert.ToDouble(c.ToString());
                i--;
            }
            return str.ToString();
        }


这篇关于如何在c#中将带二进制数的字符串列表转换为十进制数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 14:52