Author Topic: Cnovert to binary  (Read 863 times)

0 Members and 1 Guest are viewing this topic.

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Cnovert to binary
« on: August 02, 2014, 01:32:36 am »
I am trying to make my own algorithm for converting to binary from decade system. But somehow my code doesn't work. Here it is: http://pastebin.com/9SEgT56i

If you don't understand, let me explain. Variable ValueInteger is number passed by function (it's a decade number but passed as string). Variable size is size of array remainder. I have also the variable (String) that take this number, and later use it to convert string to int. So, when I do all that, I try simply idea. Repeat loop until that same number don't become 0 and it should become 0, because as we divide number with 2, in one moment it must be 0. Inside this loop, I take each reminder in array with statement :

remainder[i++] = ValueInteger % 2;

That sound quite logical to me and it should work. After that I simply divide original number with 2 and continue to loop again. After loop is done, I try to reverse values within array (because we read binary number in opposite direction). AAAND, after that I try to present results, but whenever I do that, there is this error:

Vae Victis - suffering to the conquered

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Cnovert to binary
« Reply #1 on: August 02, 2014, 08:35:20 am »
That "remainder[i++]" looks weird to me.
It looks like the IDE is throwing this error because you keep increasing the index in an undetermined length loop (meaning it'll go until it reaches 0).
You define the index as:

size = number.Length;
int[] remainder = new int[size];

which is static typing, and that error simply tells you that you didn't handle the exception that COULD BE thrown during runtime if the loop goes above what you had declared the remainder array to be.

To solve such a problem I'd use a dynamic array, such as an ArrayList (in Java).
« Last Edit: August 02, 2014, 08:36:14 am by Kulverstukas »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Cnovert to binary
« Reply #2 on: August 02, 2014, 08:43:45 am »
Code: [Select]
size = number.Length;
int[] remainder = new int[size];

You save the binary number in the remainder. The binary number needs more digits than the decimal one. So it is wrong to assume that the length is equal.
If I remember correctly the length of a binary number is:
floor(log decnumber) + 1

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: Cnovert to binary
« Reply #3 on: August 02, 2014, 10:29:50 am »
Oh my god. Can't believe what I set as array size. I mean, am I blind or what. Since when four digital number is converted to binary as well four digit? What a noobish overthink. Thanks guys, I will try to change this as soon as I reach PC.
Vae Victis - suffering to the conquered