-3

I have a number that I store in string because it is a long number and I want to convert it to binary format.

string number = "25274132531129322906392322409257377862778880"

I want to get:

string binresult;

that holds value:

 0000000000000001001000100010001000000000000000100000001011101000000000000000000011110000000000000000110100000000000000100000000000000000000000000000000000000000
12
  • BigInteger convert to base Commented Feb 12, 2013 at 15:25
  • 1
    Also homework is about proving you can google things, not getting people on the internet to do it for you. We all did this assignment and it will teach you the value of Microsoft's MSDN site. Please take your classes seriously I want to work with people who can actually solve a problem not those who beg for my help. Commented Feb 12, 2013 at 15:27
  • This question stackoverflow.com/questions/2652760/… about converting big numbers to hex should work for you, with a few minor tweaks to produce binary instead. Commented Feb 12, 2013 at 15:29
  • I checked about 300 pages and found no solution, BigInteger can't do it btw and i don't have .NET 4.5. The page Michael Edenfield provided has nothing to do with binary. Commented Feb 12, 2013 at 15:30
  • @Sparksis I thought the point was to learn how to do it, not look up how to do it. I didn't have the internets to use when I did this assignment. :) Of course, that was around 1987 and integers weren't as big. Commented Feb 12, 2013 at 15:32

3 Answers 3

1

You actually can use Dan Byström's answer to How to convert a gi-normous integer (in string format) to hex format? (C#), like Michael Edenfeld suggested in the comments. It will get you from your large number string to hexadecimal. I will repost his code for your convenience:

var s = "25274132531129322906392322409257377862778880";
var result = new List<byte>();
result.Add(0);
foreach (char c in s)
{
    int val = (int)(c - '0');
    for (int i = 0 ; i < result.Count ; i++)
    {
        int digit = result[i] * 10 + val;
        result[i] = (byte)(digit & 0x0F);
        val = digit >> 4;
    }
    if (val != 0)
        result.Add((byte)val);
}

var hex = "";
foreach (byte b in result)
    hex = "0123456789ABCDEF"[b] + hex;

I won't profess to understand how he came up with this, but I have tested it with a few numbers that I know the hex values for, and it appears to work. I'd love to hear an explanation from him.

Now, you want your answer in binary, and luckily for you, converting from hexadecimal to binary is one of the easier things in life. Every hex value converts directly to its 4-bit binary counterpart (0x7 = 0111, 0xA = 1010, etc). You could write a loop to perform this conversion character by character, but Guffa's answer to C# how convert large HEX string to binary provides this handy dandy Linq statement to perform the same action:

string binaryString = string.Join(string.Empty,
hex.Select(c => Convert.ToString(Convert.ToInt32(
c.ToString(), 16), 2).PadLeft(4, '0')));

Running all of this together gives me:

01110001010101010100100000000000110010010010001010100000000000000101110111000000000001010001010000000000110001111111111111111111111111111111111011100010

For your number. I don't know how you plan to verify it, but if Dan's hex translation is right, then that binary string will definitely be right.

Sign up to request clarification or add additional context in comments.

3 Comments

I know my number is 100% right, even more now that i found a solution to this in the meanwhile. If this gives you the binary answer you typed below it ain't calculating right. Btw thx for trying.
Out of curiosity, did you end up implementing your own string math?
I did support around large numbers on my own for some time now. I did this because i didn't want to use those of Microsoft, because it is dependent on .NET versions. I wrote everything from start to be able to use it with any .NET version and now wanted to add also conversions to different formats.
0

System.Convert can help you with bits and pieces like this. See below.

string decString = "12345";
int integer = Convert.ToInt32(decString);
string binString = Convert.ToString(integer, 2);

2 Comments

int can't hold 25274132531129322906392322409257377862778880
@tjohn - Just replace int for int64, its pretty clear, the binary value your trying to load is a 64-bits integer. The only important values within the value actually is 0000000000000001001000100010001000000000000000100000001011101000000000000000000011110000000000000000110100000000000000100000000000000000000000000000000000000000 if the leading 0's were anything except 000000000000000 then the number cannot be a int32 or int64
0

You can try this:

string number = "25274132531129322906392322409257377862778880";
char[] strArr = number.ToCharArray();
StringBuilder sb = new StringBuilder();
foreach (char chr in strArr)
{
    sb.Append(Convert.ToString((int)Char.GetNumericValue(chr), 2));
}
string binresult = sb.ToString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.