-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
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
tjohn
  • 3
  • 1
  • 4
  • BigInteger convert to base – Colton Feb 12 '13 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. – Colton Feb 12 '13 at 15:27
  • This question http://stackoverflow.com/questions/2652760/how-to-convert-a-gi-normous-integer-in-string-format-to-hex-format-c?rq=1 about converting big numbers to hex should work for you, with a few minor tweaks to produce binary instead. – Michael Edenfield Feb 12 '13 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. – tjohn Feb 12 '13 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. – John Kraft Feb 12 '13 at 15:32
  • It is funny how people vote down my question and call me a school boy when they obviously don't know the solution to this. – tjohn Feb 12 '13 at 15:34
  • @JohnKraft I am saying it's about becoming familiar with resources available. The JavaDOCS and MSDN libraries should be a developers first stop when looking for a solution if they understand the language and architecture they are developing with. Not going to lie I worked with a developer from 1984, he refused to change his development tactics from brute-force "solution solving" and it left some nasty security holes. I'm assuming that by you being here you're quite a bit better than that, through. – Colton Feb 12 '13 at 15:41
  • @tjohn as I said. We've all done it. This is CS 101. If you can't solve a basic problem by googling or again going to MSDN!!!!!!!!! then you will not be able to handle the more complex issues. michael-edenfield already provided you with a solution, thought it's not the one I'd use it will work. – Colton Feb 12 '13 at 15:44
  • @Sparksis I was being a bit facetious, but there seems to be a rash of new devs coming out of school that don't know how to do anything. They can Google or SO, and copy/paste stuff; but don't understand what they are copying, and can't begin to troubleshoot it. – John Kraft Feb 12 '13 at 15:48
  • This is hard and probably can't be solved by 99% teachers of programming languages, not to mention as easy as you make it to be Sparksis. – tjohn Feb 12 '13 at 16:00
  • @tjohn - If this binary number is larger then 64-bits then `BigInteger` is your **ONLY** solution. The structure exists in `.NET Framework 4.0` and `.NET Framework 4.5` please do your research before making statements like you have made. This number has to be signed. – Security Hound Feb 12 '13 at 17:29
  • @JohnKraft I completely agree. It's upsetting to see the number of "Java Developers" or "C# Developers" that don't understand that their software runs on top of a vm (of sorts). I've met several diploma mill graduates and it's hard to work with those types as well. – Colton Feb 22 '13 at 22:36

3 Answers3

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.

Community
  • 1
  • 1
jszigeti
  • 373
  • 4
  • 11
  • 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. – tjohn Feb 12 '13 at 17:50
  • Out of curiosity, did you end up implementing your own string math? – jszigeti Feb 12 '13 at 17:51
  • 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. – tjohn Feb 12 '13 at 18:24
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();
jszigeti
  • 373
  • 4
  • 11
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
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);
Uatec
  • 141
  • 4
  • int can't hold 25274132531129322906392322409257377862778880 – tjohn Feb 12 '13 at 15:56
  • @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` – Security Hound Feb 12 '13 at 17:23