C: Converting a very large integer into a binary











up vote
0
down vote

favorite
1












I am trying to convert decimal Nos. into binary. The code works pretty fine (Windows 7 , 32 bit MS-VS2010):



int main()
{

int k, n;
int binary[100];
printf("Enter the value in decimal n ");
scanf("%d", &k);
n = (log(k * 1.0) / log(2 * 1.0)) + 1 ; //total no. of binary bits in this decimal

for (int i = n; i > 0; i--)
{
binary[i] = k % 2;
k /= 2;
}

return 0;

}


But the limitation is that it works for Int size values only i.e. 32 bit. I want to modify this code so that it works for 2048 bits (decimal numbers containing 617 digits actually). I am not allowed to use any library.



Can someone give me some pointers how to proceed to tackle this?



Can someone give an example code snippet say for 64 bits ? Then I can use this to extend to higher values.



Update



1-As per suggestions I am trying to use strings. But I am not able to understand how to convert an String into large Int (I cant use stoi() as thsi will convert to 32 bit int , right? ).



2- Secondly I have to find:



log(222121212213212313133123413131313131311313154515441315413451315641314563154134156313461316413415635154613415645156451434)   


Is the library function log capable of finding this ? Then what is the solution?










share|improve this question




















  • 5




    Read the number as a string instead of an int. Then process the digits in the string.
    – Housy
    Dec 18 '14 at 13:24






  • 2




    You don't need a library, just take the input string and convert.
    – 2501
    Dec 18 '14 at 13:25






  • 3




    If you're learning, there's no point in using libraries as there's not much you'll learn. You'll just get the end result directly and things will seem like magic, you wouldn't know how it happened. However, for production code, instead of reinventing the wheel, maintaining the bugs that ensue, using a time-tested library, if that's an option, is the way to go. Even then, you should choose a library and use it with some knowledge on what it does.
    – legends2k
    Dec 18 '14 at 13:28








  • 1




    Here are some excellent pointers.
    – bitmask
    Dec 18 '14 at 14:24






  • 1




    You're already using libc and libm if you have printf, scanf, and log, so what are your actual restrictions, just the stdlib?
    – Ryan Haining
    Dec 18 '14 at 16:19















up vote
0
down vote

favorite
1












I am trying to convert decimal Nos. into binary. The code works pretty fine (Windows 7 , 32 bit MS-VS2010):



int main()
{

int k, n;
int binary[100];
printf("Enter the value in decimal n ");
scanf("%d", &k);
n = (log(k * 1.0) / log(2 * 1.0)) + 1 ; //total no. of binary bits in this decimal

for (int i = n; i > 0; i--)
{
binary[i] = k % 2;
k /= 2;
}

return 0;

}


But the limitation is that it works for Int size values only i.e. 32 bit. I want to modify this code so that it works for 2048 bits (decimal numbers containing 617 digits actually). I am not allowed to use any library.



Can someone give me some pointers how to proceed to tackle this?



Can someone give an example code snippet say for 64 bits ? Then I can use this to extend to higher values.



Update



1-As per suggestions I am trying to use strings. But I am not able to understand how to convert an String into large Int (I cant use stoi() as thsi will convert to 32 bit int , right? ).



2- Secondly I have to find:



log(222121212213212313133123413131313131311313154515441315413451315641314563154134156313461316413415635154613415645156451434)   


Is the library function log capable of finding this ? Then what is the solution?










share|improve this question




















  • 5




    Read the number as a string instead of an int. Then process the digits in the string.
    – Housy
    Dec 18 '14 at 13:24






  • 2




    You don't need a library, just take the input string and convert.
    – 2501
    Dec 18 '14 at 13:25






  • 3




    If you're learning, there's no point in using libraries as there's not much you'll learn. You'll just get the end result directly and things will seem like magic, you wouldn't know how it happened. However, for production code, instead of reinventing the wheel, maintaining the bugs that ensue, using a time-tested library, if that's an option, is the way to go. Even then, you should choose a library and use it with some knowledge on what it does.
    – legends2k
    Dec 18 '14 at 13:28








  • 1




    Here are some excellent pointers.
    – bitmask
    Dec 18 '14 at 14:24






  • 1




    You're already using libc and libm if you have printf, scanf, and log, so what are your actual restrictions, just the stdlib?
    – Ryan Haining
    Dec 18 '14 at 16:19













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I am trying to convert decimal Nos. into binary. The code works pretty fine (Windows 7 , 32 bit MS-VS2010):



int main()
{

int k, n;
int binary[100];
printf("Enter the value in decimal n ");
scanf("%d", &k);
n = (log(k * 1.0) / log(2 * 1.0)) + 1 ; //total no. of binary bits in this decimal

for (int i = n; i > 0; i--)
{
binary[i] = k % 2;
k /= 2;
}

return 0;

}


But the limitation is that it works for Int size values only i.e. 32 bit. I want to modify this code so that it works for 2048 bits (decimal numbers containing 617 digits actually). I am not allowed to use any library.



Can someone give me some pointers how to proceed to tackle this?



Can someone give an example code snippet say for 64 bits ? Then I can use this to extend to higher values.



Update



1-As per suggestions I am trying to use strings. But I am not able to understand how to convert an String into large Int (I cant use stoi() as thsi will convert to 32 bit int , right? ).



2- Secondly I have to find:



log(222121212213212313133123413131313131311313154515441315413451315641314563154134156313461316413415635154613415645156451434)   


Is the library function log capable of finding this ? Then what is the solution?










share|improve this question















I am trying to convert decimal Nos. into binary. The code works pretty fine (Windows 7 , 32 bit MS-VS2010):



int main()
{

int k, n;
int binary[100];
printf("Enter the value in decimal n ");
scanf("%d", &k);
n = (log(k * 1.0) / log(2 * 1.0)) + 1 ; //total no. of binary bits in this decimal

for (int i = n; i > 0; i--)
{
binary[i] = k % 2;
k /= 2;
}

return 0;

}


But the limitation is that it works for Int size values only i.e. 32 bit. I want to modify this code so that it works for 2048 bits (decimal numbers containing 617 digits actually). I am not allowed to use any library.



Can someone give me some pointers how to proceed to tackle this?



Can someone give an example code snippet say for 64 bits ? Then I can use this to extend to higher values.



Update



1-As per suggestions I am trying to use strings. But I am not able to understand how to convert an String into large Int (I cant use stoi() as thsi will convert to 32 bit int , right? ).



2- Secondly I have to find:



log(222121212213212313133123413131313131311313154515441315413451315641314563154134156313461316413415635154613415645156451434)   


Is the library function log capable of finding this ? Then what is the solution?







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 18 '14 at 14:33

























asked Dec 18 '14 at 13:22









user3891236

332314




332314








  • 5




    Read the number as a string instead of an int. Then process the digits in the string.
    – Housy
    Dec 18 '14 at 13:24






  • 2




    You don't need a library, just take the input string and convert.
    – 2501
    Dec 18 '14 at 13:25






  • 3




    If you're learning, there's no point in using libraries as there's not much you'll learn. You'll just get the end result directly and things will seem like magic, you wouldn't know how it happened. However, for production code, instead of reinventing the wheel, maintaining the bugs that ensue, using a time-tested library, if that's an option, is the way to go. Even then, you should choose a library and use it with some knowledge on what it does.
    – legends2k
    Dec 18 '14 at 13:28








  • 1




    Here are some excellent pointers.
    – bitmask
    Dec 18 '14 at 14:24






  • 1




    You're already using libc and libm if you have printf, scanf, and log, so what are your actual restrictions, just the stdlib?
    – Ryan Haining
    Dec 18 '14 at 16:19














  • 5




    Read the number as a string instead of an int. Then process the digits in the string.
    – Housy
    Dec 18 '14 at 13:24






  • 2




    You don't need a library, just take the input string and convert.
    – 2501
    Dec 18 '14 at 13:25






  • 3




    If you're learning, there's no point in using libraries as there's not much you'll learn. You'll just get the end result directly and things will seem like magic, you wouldn't know how it happened. However, for production code, instead of reinventing the wheel, maintaining the bugs that ensue, using a time-tested library, if that's an option, is the way to go. Even then, you should choose a library and use it with some knowledge on what it does.
    – legends2k
    Dec 18 '14 at 13:28








  • 1




    Here are some excellent pointers.
    – bitmask
    Dec 18 '14 at 14:24






  • 1




    You're already using libc and libm if you have printf, scanf, and log, so what are your actual restrictions, just the stdlib?
    – Ryan Haining
    Dec 18 '14 at 16:19








5




5




Read the number as a string instead of an int. Then process the digits in the string.
– Housy
Dec 18 '14 at 13:24




Read the number as a string instead of an int. Then process the digits in the string.
– Housy
Dec 18 '14 at 13:24




2




2




You don't need a library, just take the input string and convert.
– 2501
Dec 18 '14 at 13:25




You don't need a library, just take the input string and convert.
– 2501
Dec 18 '14 at 13:25




3




3




If you're learning, there's no point in using libraries as there's not much you'll learn. You'll just get the end result directly and things will seem like magic, you wouldn't know how it happened. However, for production code, instead of reinventing the wheel, maintaining the bugs that ensue, using a time-tested library, if that's an option, is the way to go. Even then, you should choose a library and use it with some knowledge on what it does.
– legends2k
Dec 18 '14 at 13:28






If you're learning, there's no point in using libraries as there's not much you'll learn. You'll just get the end result directly and things will seem like magic, you wouldn't know how it happened. However, for production code, instead of reinventing the wheel, maintaining the bugs that ensue, using a time-tested library, if that's an option, is the way to go. Even then, you should choose a library and use it with some knowledge on what it does.
– legends2k
Dec 18 '14 at 13:28






1




1




Here are some excellent pointers.
– bitmask
Dec 18 '14 at 14:24




Here are some excellent pointers.
– bitmask
Dec 18 '14 at 14:24




1




1




You're already using libc and libm if you have printf, scanf, and log, so what are your actual restrictions, just the stdlib?
– Ryan Haining
Dec 18 '14 at 16:19




You're already using libc and libm if you have printf, scanf, and log, so what are your actual restrictions, just the stdlib?
– Ryan Haining
Dec 18 '14 at 16:19












3 Answers
3






active

oldest

votes

















up vote
1
down vote













Since you told that you just need some pointers and not the actual answer, here goes:




I am not able to understand how to convert an String into large Int




That's because you can't. If you want to convert a number that huge to a numerical type, in the first place you need such a type that can hold numbers that big. The language doesn't provide you anything more than long long which is usually 128-bits long (i.e. if you can use C99, or just long which is usually lesser than a long long). Since your tutor told you not to use any external library, it's a clear sign that s/he wants you to code the solution using what's available only in the language and perhaps additionally the standard library.




Is the library function log capable of finding this




No, you can't use stoi or log since all of these expect arguments of some arithmetic type, while none of those built-in types are that big to hold numbers this huge. So you've to work completely with strings (i.e. either static or dynamic char buffers).



I understand that you want to use log to deduce the number of digits the binary output would need; but there's another option, which is to not know the number of digits before hand and allocate them dynamically with some upper bound so that you needn't re-allocate them further.



Lets take an example.




  1. Allocate 3 char buffers in, out (length of input) and bin (length of input * 4).

  2. Copy input to in

  3. While in is not "0" or "1" do else goto 12

  4. For each element ch in in do else goto 10

  5. Convert ch to integer i

  6. If is_odd = 1 then i += 10

  7. quot = i / 2

  8. Append quot to out


  9. is_odd = quot % 2; goto 4

  10. If is_odd = 1 append '1' else '0' to bin

  11. Copy out to in, reset out and goto 3

  12. Append in to bin

  13. Print bin in reverse


When you integer divide a number by 2, the quotient would always be less than or equal to the number of digits of the dividend. So you could allocate in and out with the same size as the input and use it for all iterations. For the bin buffer, the knowledge that each decimal digit wouldn't take more than 4 bits (9 takes a nibble, 1001) would help. So if the input is 10 digits, then 10*4 = 40 bytes would be the upper limit needed for bin buffer and 10 bytes would be needed for the in and out buffers.



This is a vague write-up of an algorithm, I hope it conveys the idea. I feel writing code is more easier than writing algorithms properly.






share|improve this answer






























    up vote
    0
    down vote













    I'm afraid there are no standard types in C that will allow you to store such a big value with 20148 bits... You can try to read the string from console (not converting into int), and then parse the string into "010101...." on your own.



    The approach would be like that:

    You should go for "dividing" the string by 2 in each step (for each division by 2 you need to divide all digits of the string by 2, and handle special cases like 11 / 2 => 5), and for each step if the value cannot be divided by 2, then you then you can put "1" as another binary digit, otherwise you put "0". This way you gather the digits '0', '1', '0', '1', etc. one by one. Then finally you need to reverse the order of digits. A similar approach implemented in C# you can find here: Decimal to binary conversion in c #






    share|improve this answer






























      up vote
      0
      down vote













      Regarding the update:



      Grinding it through WolframAlpha gives:




      log(222121212213212313133123413131313131311313154515441315413451315641314563154134156313461316413415635154613415645156451434)




      is roughly




      274.8056791141317511022806994521207149274321589939103691837589..




      Test:



      Putting it into exp gives:




      2.2212121221321231313312341313131313131131315451544131541.. × 10^119




      This raises the question about the precision you need.



      Note: I assumed you mean the natural logarithm (base e).






      share|improve this answer























      • Basically that log is for finding total no. of bits in a decimal no. that we enter. And this is log base 10. I want to do it in my C program, but log cant handle such big numbers.
        – user3891236
        Dec 19 '14 at 6:06











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27547605%2fc-converting-a-very-large-integer-into-a-binary%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      Since you told that you just need some pointers and not the actual answer, here goes:




      I am not able to understand how to convert an String into large Int




      That's because you can't. If you want to convert a number that huge to a numerical type, in the first place you need such a type that can hold numbers that big. The language doesn't provide you anything more than long long which is usually 128-bits long (i.e. if you can use C99, or just long which is usually lesser than a long long). Since your tutor told you not to use any external library, it's a clear sign that s/he wants you to code the solution using what's available only in the language and perhaps additionally the standard library.




      Is the library function log capable of finding this




      No, you can't use stoi or log since all of these expect arguments of some arithmetic type, while none of those built-in types are that big to hold numbers this huge. So you've to work completely with strings (i.e. either static or dynamic char buffers).



      I understand that you want to use log to deduce the number of digits the binary output would need; but there's another option, which is to not know the number of digits before hand and allocate them dynamically with some upper bound so that you needn't re-allocate them further.



      Lets take an example.




      1. Allocate 3 char buffers in, out (length of input) and bin (length of input * 4).

      2. Copy input to in

      3. While in is not "0" or "1" do else goto 12

      4. For each element ch in in do else goto 10

      5. Convert ch to integer i

      6. If is_odd = 1 then i += 10

      7. quot = i / 2

      8. Append quot to out


      9. is_odd = quot % 2; goto 4

      10. If is_odd = 1 append '1' else '0' to bin

      11. Copy out to in, reset out and goto 3

      12. Append in to bin

      13. Print bin in reverse


      When you integer divide a number by 2, the quotient would always be less than or equal to the number of digits of the dividend. So you could allocate in and out with the same size as the input and use it for all iterations. For the bin buffer, the knowledge that each decimal digit wouldn't take more than 4 bits (9 takes a nibble, 1001) would help. So if the input is 10 digits, then 10*4 = 40 bytes would be the upper limit needed for bin buffer and 10 bytes would be needed for the in and out buffers.



      This is a vague write-up of an algorithm, I hope it conveys the idea. I feel writing code is more easier than writing algorithms properly.






      share|improve this answer



























        up vote
        1
        down vote













        Since you told that you just need some pointers and not the actual answer, here goes:




        I am not able to understand how to convert an String into large Int




        That's because you can't. If you want to convert a number that huge to a numerical type, in the first place you need such a type that can hold numbers that big. The language doesn't provide you anything more than long long which is usually 128-bits long (i.e. if you can use C99, or just long which is usually lesser than a long long). Since your tutor told you not to use any external library, it's a clear sign that s/he wants you to code the solution using what's available only in the language and perhaps additionally the standard library.




        Is the library function log capable of finding this




        No, you can't use stoi or log since all of these expect arguments of some arithmetic type, while none of those built-in types are that big to hold numbers this huge. So you've to work completely with strings (i.e. either static or dynamic char buffers).



        I understand that you want to use log to deduce the number of digits the binary output would need; but there's another option, which is to not know the number of digits before hand and allocate them dynamically with some upper bound so that you needn't re-allocate them further.



        Lets take an example.




        1. Allocate 3 char buffers in, out (length of input) and bin (length of input * 4).

        2. Copy input to in

        3. While in is not "0" or "1" do else goto 12

        4. For each element ch in in do else goto 10

        5. Convert ch to integer i

        6. If is_odd = 1 then i += 10

        7. quot = i / 2

        8. Append quot to out


        9. is_odd = quot % 2; goto 4

        10. If is_odd = 1 append '1' else '0' to bin

        11. Copy out to in, reset out and goto 3

        12. Append in to bin

        13. Print bin in reverse


        When you integer divide a number by 2, the quotient would always be less than or equal to the number of digits of the dividend. So you could allocate in and out with the same size as the input and use it for all iterations. For the bin buffer, the knowledge that each decimal digit wouldn't take more than 4 bits (9 takes a nibble, 1001) would help. So if the input is 10 digits, then 10*4 = 40 bytes would be the upper limit needed for bin buffer and 10 bytes would be needed for the in and out buffers.



        This is a vague write-up of an algorithm, I hope it conveys the idea. I feel writing code is more easier than writing algorithms properly.






        share|improve this answer

























          up vote
          1
          down vote










          up vote
          1
          down vote









          Since you told that you just need some pointers and not the actual answer, here goes:




          I am not able to understand how to convert an String into large Int




          That's because you can't. If you want to convert a number that huge to a numerical type, in the first place you need such a type that can hold numbers that big. The language doesn't provide you anything more than long long which is usually 128-bits long (i.e. if you can use C99, or just long which is usually lesser than a long long). Since your tutor told you not to use any external library, it's a clear sign that s/he wants you to code the solution using what's available only in the language and perhaps additionally the standard library.




          Is the library function log capable of finding this




          No, you can't use stoi or log since all of these expect arguments of some arithmetic type, while none of those built-in types are that big to hold numbers this huge. So you've to work completely with strings (i.e. either static or dynamic char buffers).



          I understand that you want to use log to deduce the number of digits the binary output would need; but there's another option, which is to not know the number of digits before hand and allocate them dynamically with some upper bound so that you needn't re-allocate them further.



          Lets take an example.




          1. Allocate 3 char buffers in, out (length of input) and bin (length of input * 4).

          2. Copy input to in

          3. While in is not "0" or "1" do else goto 12

          4. For each element ch in in do else goto 10

          5. Convert ch to integer i

          6. If is_odd = 1 then i += 10

          7. quot = i / 2

          8. Append quot to out


          9. is_odd = quot % 2; goto 4

          10. If is_odd = 1 append '1' else '0' to bin

          11. Copy out to in, reset out and goto 3

          12. Append in to bin

          13. Print bin in reverse


          When you integer divide a number by 2, the quotient would always be less than or equal to the number of digits of the dividend. So you could allocate in and out with the same size as the input and use it for all iterations. For the bin buffer, the knowledge that each decimal digit wouldn't take more than 4 bits (9 takes a nibble, 1001) would help. So if the input is 10 digits, then 10*4 = 40 bytes would be the upper limit needed for bin buffer and 10 bytes would be needed for the in and out buffers.



          This is a vague write-up of an algorithm, I hope it conveys the idea. I feel writing code is more easier than writing algorithms properly.






          share|improve this answer














          Since you told that you just need some pointers and not the actual answer, here goes:




          I am not able to understand how to convert an String into large Int




          That's because you can't. If you want to convert a number that huge to a numerical type, in the first place you need such a type that can hold numbers that big. The language doesn't provide you anything more than long long which is usually 128-bits long (i.e. if you can use C99, or just long which is usually lesser than a long long). Since your tutor told you not to use any external library, it's a clear sign that s/he wants you to code the solution using what's available only in the language and perhaps additionally the standard library.




          Is the library function log capable of finding this




          No, you can't use stoi or log since all of these expect arguments of some arithmetic type, while none of those built-in types are that big to hold numbers this huge. So you've to work completely with strings (i.e. either static or dynamic char buffers).



          I understand that you want to use log to deduce the number of digits the binary output would need; but there's another option, which is to not know the number of digits before hand and allocate them dynamically with some upper bound so that you needn't re-allocate them further.



          Lets take an example.




          1. Allocate 3 char buffers in, out (length of input) and bin (length of input * 4).

          2. Copy input to in

          3. While in is not "0" or "1" do else goto 12

          4. For each element ch in in do else goto 10

          5. Convert ch to integer i

          6. If is_odd = 1 then i += 10

          7. quot = i / 2

          8. Append quot to out


          9. is_odd = quot % 2; goto 4

          10. If is_odd = 1 append '1' else '0' to bin

          11. Copy out to in, reset out and goto 3

          12. Append in to bin

          13. Print bin in reverse


          When you integer divide a number by 2, the quotient would always be less than or equal to the number of digits of the dividend. So you could allocate in and out with the same size as the input and use it for all iterations. For the bin buffer, the knowledge that each decimal digit wouldn't take more than 4 bits (9 takes a nibble, 1001) would help. So if the input is 10 digits, then 10*4 = 40 bytes would be the upper limit needed for bin buffer and 10 bytes would be needed for the in and out buffers.



          This is a vague write-up of an algorithm, I hope it conveys the idea. I feel writing code is more easier than writing algorithms properly.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 18 '14 at 16:44

























          answered Dec 18 '14 at 15:52









          legends2k

          18.7k1572151




          18.7k1572151
























              up vote
              0
              down vote













              I'm afraid there are no standard types in C that will allow you to store such a big value with 20148 bits... You can try to read the string from console (not converting into int), and then parse the string into "010101...." on your own.



              The approach would be like that:

              You should go for "dividing" the string by 2 in each step (for each division by 2 you need to divide all digits of the string by 2, and handle special cases like 11 / 2 => 5), and for each step if the value cannot be divided by 2, then you then you can put "1" as another binary digit, otherwise you put "0". This way you gather the digits '0', '1', '0', '1', etc. one by one. Then finally you need to reverse the order of digits. A similar approach implemented in C# you can find here: Decimal to binary conversion in c #






              share|improve this answer



























                up vote
                0
                down vote













                I'm afraid there are no standard types in C that will allow you to store such a big value with 20148 bits... You can try to read the string from console (not converting into int), and then parse the string into "010101...." on your own.



                The approach would be like that:

                You should go for "dividing" the string by 2 in each step (for each division by 2 you need to divide all digits of the string by 2, and handle special cases like 11 / 2 => 5), and for each step if the value cannot be divided by 2, then you then you can put "1" as another binary digit, otherwise you put "0". This way you gather the digits '0', '1', '0', '1', etc. one by one. Then finally you need to reverse the order of digits. A similar approach implemented in C# you can find here: Decimal to binary conversion in c #






                share|improve this answer

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  I'm afraid there are no standard types in C that will allow you to store such a big value with 20148 bits... You can try to read the string from console (not converting into int), and then parse the string into "010101...." on your own.



                  The approach would be like that:

                  You should go for "dividing" the string by 2 in each step (for each division by 2 you need to divide all digits of the string by 2, and handle special cases like 11 / 2 => 5), and for each step if the value cannot be divided by 2, then you then you can put "1" as another binary digit, otherwise you put "0". This way you gather the digits '0', '1', '0', '1', etc. one by one. Then finally you need to reverse the order of digits. A similar approach implemented in C# you can find here: Decimal to binary conversion in c #






                  share|improve this answer














                  I'm afraid there are no standard types in C that will allow you to store such a big value with 20148 bits... You can try to read the string from console (not converting into int), and then parse the string into "010101...." on your own.



                  The approach would be like that:

                  You should go for "dividing" the string by 2 in each step (for each division by 2 you need to divide all digits of the string by 2, and handle special cases like 11 / 2 => 5), and for each step if the value cannot be divided by 2, then you then you can put "1" as another binary digit, otherwise you put "0". This way you gather the digits '0', '1', '0', '1', etc. one by one. Then finally you need to reverse the order of digits. A similar approach implemented in C# you can find here: Decimal to binary conversion in c #







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 23 '17 at 12:32









                  Community

                  11




                  11










                  answered Dec 18 '14 at 13:30









                  msporek

                  909415




                  909415






















                      up vote
                      0
                      down vote













                      Regarding the update:



                      Grinding it through WolframAlpha gives:




                      log(222121212213212313133123413131313131311313154515441315413451315641314563154134156313461316413415635154613415645156451434)




                      is roughly




                      274.8056791141317511022806994521207149274321589939103691837589..




                      Test:



                      Putting it into exp gives:




                      2.2212121221321231313312341313131313131131315451544131541.. × 10^119




                      This raises the question about the precision you need.



                      Note: I assumed you mean the natural logarithm (base e).






                      share|improve this answer























                      • Basically that log is for finding total no. of bits in a decimal no. that we enter. And this is log base 10. I want to do it in my C program, but log cant handle such big numbers.
                        – user3891236
                        Dec 19 '14 at 6:06















                      up vote
                      0
                      down vote













                      Regarding the update:



                      Grinding it through WolframAlpha gives:




                      log(222121212213212313133123413131313131311313154515441315413451315641314563154134156313461316413415635154613415645156451434)




                      is roughly




                      274.8056791141317511022806994521207149274321589939103691837589..




                      Test:



                      Putting it into exp gives:




                      2.2212121221321231313312341313131313131131315451544131541.. × 10^119




                      This raises the question about the precision you need.



                      Note: I assumed you mean the natural logarithm (base e).






                      share|improve this answer























                      • Basically that log is for finding total no. of bits in a decimal no. that we enter. And this is log base 10. I want to do it in my C program, but log cant handle such big numbers.
                        – user3891236
                        Dec 19 '14 at 6:06













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      Regarding the update:



                      Grinding it through WolframAlpha gives:




                      log(222121212213212313133123413131313131311313154515441315413451315641314563154134156313461316413415635154613415645156451434)




                      is roughly




                      274.8056791141317511022806994521207149274321589939103691837589..




                      Test:



                      Putting it into exp gives:




                      2.2212121221321231313312341313131313131131315451544131541.. × 10^119




                      This raises the question about the precision you need.



                      Note: I assumed you mean the natural logarithm (base e).






                      share|improve this answer














                      Regarding the update:



                      Grinding it through WolframAlpha gives:




                      log(222121212213212313133123413131313131311313154515441315413451315641314563154134156313461316413415635154613415645156451434)




                      is roughly




                      274.8056791141317511022806994521207149274321589939103691837589..




                      Test:



                      Putting it into exp gives:




                      2.2212121221321231313312341313131313131131315451544131541.. × 10^119




                      This raises the question about the precision you need.



                      Note: I assumed you mean the natural logarithm (base e).







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 18 '14 at 16:22

























                      answered Dec 18 '14 at 16:16









                      mvw

                      4,03711827




                      4,03711827












                      • Basically that log is for finding total no. of bits in a decimal no. that we enter. And this is log base 10. I want to do it in my C program, but log cant handle such big numbers.
                        – user3891236
                        Dec 19 '14 at 6:06


















                      • Basically that log is for finding total no. of bits in a decimal no. that we enter. And this is log base 10. I want to do it in my C program, but log cant handle such big numbers.
                        – user3891236
                        Dec 19 '14 at 6:06
















                      Basically that log is for finding total no. of bits in a decimal no. that we enter. And this is log base 10. I want to do it in my C program, but log cant handle such big numbers.
                      – user3891236
                      Dec 19 '14 at 6:06




                      Basically that log is for finding total no. of bits in a decimal no. that we enter. And this is log base 10. I want to do it in my C program, but log cant handle such big numbers.
                      – user3891236
                      Dec 19 '14 at 6:06


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27547605%2fc-converting-a-very-large-integer-into-a-binary%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Guess what letter conforming each word

                      Port of Spain

                      Run scheduled task as local user group (not BUILTIN)