Solved

double to binary in c#

Posted on 2009-05-17
15
1,791 Views
Last Modified: 2013-12-17
how can i convert the double value of 1E+27 (or bigger) to binary?

Thanks.
0
Comment
Question by:OrenRozen
  • 6
  • 4
  • 3
  • +2
15 Comments
 
LVL 5

Expert Comment

by:roxviper
ID: 24405614
I think you can find more about this here http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=54741
0
 
LVL 3

Author Comment

by:OrenRozen
ID: 24405862
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.
0
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 24406122
try using this

Convert.ToString(i, 2)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 24406214
What are you attempting to accomplish? Are you trying to print the value in binary format?
0
 
LVL 3

Author Comment

by:OrenRozen
ID: 24406742
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.
0
 
LVL 9

Expert Comment

by:tculler
ID: 24407764
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
0
 
LVL 3

Author Comment

by:OrenRozen
ID: 24409079
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

0
 
LVL 9

Expert Comment

by:tculler
ID: 24412484
Heh... Have you heard of arrays OrenRozen?
0
 
LVL 3

Author Comment

by:OrenRozen
ID: 24412596
can you show me a small example of how arrays can solve my problem?
0
 
LVL 9

Expert Comment

by:tculler
ID: 24413856
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

0
 
LVL 3

Author Comment

by:OrenRozen
ID: 24419500
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.
0
 
LVL 9

Expert Comment

by:tculler
ID: 24432424
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.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 24433865
Hi OrenRozen;

I believe that the code snippet below will do what you want. In one of your post you have the following lines. Note that when you go from the 9th bit position to the 10th bit position it should go from 256 to 512 and not 256 to 1024.

if (checkBox9.Checked == true) p9 = ulong.Parse(Convert.ToString(256, 2));
if (checkBox10.Checked == true) p10 = ulong.Parse(Convert.ToString(1024, 2));

Fernando
private void button1_Click(object sender, EventArgs e)
{
    string NewBinVal = "";
 
    for (int idx = 32; idx > 0; idx--)
    {
        Control[] ctl = this.Controls.Find("checkBox" + idx.ToString(), true);
        CheckBox cb = ctl[0] as CheckBox;
        if (cb.Checked)
            NewBinVal += "1";
        else
            NewBinVal += "0";
    }
 
    Int64 intValue = Convert.ToInt64(NewBinVal, 2);
 
    textBox2.Text = intValue.ToString();
 
}

Open in new window

0
 
LVL 3

Author Closing Comment

by:OrenRozen
ID: 31582333
simple, elegant and easy.
Appreciate all your efforts.

Regards.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 24434666
Not a problem, glad I was able to help.  ;=)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question