Link to home
Start Free TrialLog in
Avatar of OrenRozen
OrenRozenFlag for Israel

asked on

double to binary in c#

how can i convert the double value of 1E+27 (or bigger) to binary?

Thanks.
Avatar of roxviper
roxviper
Flag of Romania image

I think you can find more about this here http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=54741
Avatar of OrenRozen

ASKER

Thanks for your answer, but it does not solve my problem.

to convert I use the following:
int intValue = Convert.ToInt32(DoubleBinVal, 2);   //DoubleBinVal = the value to convert to binary.

My problem is that 'DoubleBinVal' is out of the range of the int32.
any way to solve that?

Thanks.
Avatar of srikanthreddyn143
srikanthreddyn143

try using this

Convert.ToString(i, 2)
What are you attempting to accomplish? Are you trying to print the value in binary format?
I want to print out the result in int of any combination of processor affinity selection, for example: when selecting cores 1 to 3 the result is 15.
the problem is when selecting cores of high values, for example: 1 and 20.
the number is too big to calculate.
I'm not really sure what exactly the purpsoe of this is, but if int is too small, you can try System.Int64. This gives a larger range of numbers.

Also, if you know your answer cannot be negative, use unsigned integer value types (i.e. System.UInt64, System.UInt32, etc.)

Why are you doing this, by the way? Floating-point numbers are stored in a VERY different format than integers. For example, the number 5 in floating-point binary format looks absolutely nothing like the number 5 in integer format.

Hope I helped,
Nate
Hi,

The attached code is a sample of what I'm trying to do.

The problem begins when selecting from checkbox20 and above (with or without combination of any other checkboxes).

Hope that clears my issue.

Thanks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        ulong p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16 = 0;
        ulong p17,p18,p19,p20,p21,p22,p23,p24,p25,p26,p27,p28,p29,p30,p31,p32 = 0;
 
        private void button1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true) p1 = ulong.Parse(Convert.ToString(1, 2));
            if (checkBox2.Checked == true) p2 = ulong.Parse(Convert.ToString(2, 2));
            if (checkBox4.Checked == true) p3 = ulong.Parse(Convert.ToString(4, 2));
            if (checkBox3.Checked == true) p4 = ulong.Parse(Convert.ToString(8, 2));
            if (checkBox5.Checked == true) p5 = ulong.Parse(Convert.ToString(16, 2));
            if (checkBox6.Checked == true) p6 = ulong.Parse(Convert.ToString(32, 2));
            if (checkBox7.Checked == true) p7 = ulong.Parse(Convert.ToString(64, 2));
            if (checkBox8.Checked == true) p8 = ulong.Parse(Convert.ToString(128, 2));
            if (checkBox9.Checked == true) p9 = ulong.Parse(Convert.ToString(256, 2));
            if (checkBox10.Checked == true) p10 = ulong.Parse(Convert.ToString(1024, 2));
            if (checkBox11.Checked == true) p11 = ulong.Parse(Convert.ToString(2048, 2));
            if (checkBox12.Checked == true) p12 = ulong.Parse(Convert.ToString(4096, 2));
            if (checkBox13.Checked == true) p13 = ulong.Parse(Convert.ToString(8192, 2));
            if (checkBox14.Checked == true) p14 = ulong.Parse(Convert.ToString(16384, 2));
            if (checkBox15.Checked == true) p15 = ulong.Parse(Convert.ToString(32768, 2));
            if (checkBox16.Checked == true) p16 = ulong.Parse(Convert.ToString(65536, 2));
            if (checkBox17.Checked == true) p17 = ulong.Parse(Convert.ToString(131072, 2));
            if (checkBox18.Checked == true) p18 = ulong.Parse(Convert.ToString(262144, 2));
            if (checkBox19.Checked == true) p19 = ulong.Parse(Convert.ToString(524288, 2));
            if (checkBox20.Checked == true) p20 = ulong.Parse(Convert.ToString(1048576, 2));
            if (checkBox21.Checked == true) p21 = ulong.Parse(Convert.ToString(2097152, 2));
            if (checkBox22.Checked == true) p22 = ulong.Parse(Convert.ToString(4194304, 2));
            if (checkBox23.Checked == true) p23 = ulong.Parse(Convert.ToString(8388608, 2));
            if (checkBox24.Checked == true) p24 = ulong.Parse(Convert.ToString(16777216, 2));
            if (checkBox25.Checked == true) p25 = ulong.Parse(Convert.ToString(33554432, 2));
            if (checkBox26.Checked == true) p26 = ulong.Parse(Convert.ToString(67108864, 2));
            if (checkBox27.Checked == true) p27 = ulong.Parse(Convert.ToString(134217728, 2));
            if (checkBox28.Checked == true) p28 = ulong.Parse(Convert.ToString(268435456, 2));
            if (checkBox29.Checked == true) p29 = ulong.Parse(Convert.ToString(536870912, 2));
            if (checkBox30.Checked == true) p30 = ulong.Parse(Convert.ToString(1073741824, 2));
            if (checkBox31.Checked == true) p31 = ulong.Parse(Convert.ToString(2147483648, 2));
            if (checkBox32.Checked == true) p32 = ulong.Parse(Convert.ToString(4294967296, 2));
 
            string NewBinVal = (p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 +
                p10 + p11 + p12 + p13 + p14 + p15 + p16 + p17 + p18 + p19 +
                p20 + p21 + p22 + p23 + p24 + p25 + p26 + p27 + p28 + p29 + p30 + p31 + p32).ToString();
                
 
            int intValue = Convert.ToInt32(NewBinVal, 2);
 
            textBox2.Text = intValue.ToString();
            p1 = 0;
            p2 = 0;
            p3 = 0;
            p4 = 0;
            p5 = 0;
            p6 = 0;
            p7 = 0;
            p8 = 0;
            p9 = 0;
            p10 = 0;
            p11 = 0;
            p12 = 0;
            p13 = 0;
            p14 = 0;
            p15 = 0;
            p16 = 0;
            p17 = 0;
            p18 = 0;
            p19 = 0;
            p20 = 0;
            p21 = 0;
            p22 = 0;
            p23 = 0;
            p24 = 0;
            p25 = 0;
            p26 = 0;
            p27 = 0;
            p28 = 0;
            p29 = 0;
            p30 = 0;
            p31 = 0;
            p32 = 0;
            
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }
    }
}

Open in new window

Heh... Have you heard of arrays OrenRozen?
can you show me a small example of how arrays can solve my problem?
Well it won't solve your problem, but it'll at least fix up your, uh, "code"...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private UInt64[] numbers = new UInt64[32];
        // Load ALL of your CheckBoxes into the following array
        private CheckBox[] cbxs = new CheckBox[32];
 
        private void button1_Click(object sender, EventArgs e)
        {
            Int32 count = 0;
            foreach (CheckBox cbx in cbxs)
            {
                if (cbx.Checked)
                {
                    numbers[count] = UInt64.Parse(Convert.ToString((UInt64)Math.Pow(2.0, count), 2));
                }
                count++;
            }
 
            StringBuilder bldr = new StringBuilder();
            foreach (UInt64 num in numbers)
            { bldr.Append(num); }
            textBox2.Text = bldr.ToString();
 
            for (Int32 i = 0; i < numbers.Length; i++)
            { numbers[i] = 0; }
        }
    }
}

Open in new window

it sure is an elegant written code and I'll use it.
at list now I know how to use arrays with checkboxes, textboxes...

But, as you wrote, it wont solve my problem. Still looking.

Thanks.
I'm still very confused as to what it is you're trying to accomplish here. Do you need to work with numbers that are too large for UInt64? If you need numbers that are larger than the max of double, I don't know that I can help you. I'll show you Double's max value:

179,769,313,486,232,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000

That's System.Double's max value(System.Double.MaxValue) formatted to a numeric format. I really doubt that it is not adequate for your needs.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
simple, elegant and easy.
Appreciate all your efforts.

Regards.
Not a problem, glad I was able to help.  ;=)