Click here to Skip to main content
15,905,971 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I used this
1--------
temp = Convert.ToString(i, 10);

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

result are zero

code :
C#
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;
        }
    }
}

[Edit: MTHeffron -- Added pre tags and fixed formatting]
Posted
Updated 14-Dec-15 8:54am
v5
Comments
Matt T Heffron 14-Dec-15 12:51pm    
You haven't given us all of the information to help you.
What is the data type for the ListBinary.
It appears to be a List<string>.
BasmaSH 14-Dec-15 12:56pm    
[Deleted here and moved to original question]
Matt T Heffron 14-Dec-15 13:10pm    
Please! take this code and move it to the bottom of your original question using the "Improve question" button. Be sure to use the Code button in the question editor so that it will be correctly formatted.
(Maybe add a line like:
===============Additional==================
Between the original and added parts.)
Then delete this comment.
Richard MacCutchan 14-Dec-15 12:54pm    
What is the type, and what are the values in ListBinary?
BasmaSH 14-Dec-15 13:57pm    
The type of ListBinary is string ,it's values are binary number generated random by function Generate .

1 solution

Please try the below....this will work...


C#
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();
        }
 
Share this answer
 
Comments
Rajdeep Debnath 14-Dec-15 14:14pm    
Why the answer down voted? And this is unfortunate in code project that we should have some options to tell the reason, otherwise how will we improve.
BasmaSH 15-Dec-15 13:03pm    
Thanks a lot !
Richard Deeming 14-Dec-15 14:15pm    
No need to re-invent the wheel:
Convert.ToInt32(string, int)[^]

static string ConvertBinaryToDecimal(string binaryString)
{
return Convert.ToInt32(binaryString, 2).ToString();
}
Rajdeep Debnath 14-Dec-15 14:23pm    
Good. I didn't knew that. But instead of downvoting me after my try....you could have given the solution.....that would have been a positive gesture....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900