Decimal to binary conversion

I would like to convert decimal to binary. I want to create a method that converts a decimal to binary, and then calls this method from Main to print the result, but I am having some problems. This is the code that I still have. toBin is not working properly and I cannot understand what I'm doing wrong.

I'm trying to learn Java myself, so any help would be appreciated, including explanations if necessary. I learn JAVA through http://knowledgeblackbelt.com and this is one of the exercises. As I said, I no longer need an answer than I need an example and explanation of this process, because I do not seem to understand it. Thanks to the people who really help.

public class DecToBin { public static void main(String[] args) { System.out.println(toBin(2)); } public static int toBin(int Number){ int result = 0; while(Number > 0){ int mod = Number % 2; result = result * 1 + mod; Number /= 2; } return result; } 

}

+4
source share
5 answers

Hope this helps:

 /** * * @author X * */ public class ConvertDecimal { public void convert(int decimal , int base) { int result = 0; int multiplier = 1; while(decimal > 0) { int residue = decimal % base; decimal = decimal / base; result = result + residue * multiplier; multiplier = multiplier * 10; } System.out.println ("binary....." + result); } public static void main(String args[]) { ConvertDecimal conv = new ConvertDecimal(); conv.convert(128, 2); } } 
+9
source

Use Integer.toString(int number, int radix) .

 Integer.toString(2, 2); // will return "10" Integer.toString(2, 10); // will return "2" Integer.toString(15, 16); // will return "F" 
+10
source

It seems like your problem is that you add your + 1 to the result every time you add a digit to the bin value. I would probably concatenate the string and then parse it to a number. sort of:

  string result = ""; while(Number > 0){ int mod = Number % 2; result = result + mod; Number /= 2; } 
+2
source

You can use the built-in method :

 Integer.toBinaryString(i); 
+1
source

Well, the idea of ​​SOF is to help people think, and not solve their homework, so I will describe this code that I did in C, you can translate it into java, if you want, it is important that you understand how the algorithm works.

1.- I declare some variables i (counter), base (new base 2-16), word[ 32 ] (an array with 32 elements (0-31) here we will store the new number) and number (the number that the user wants to convert )

2.- I enter the values ​​for number and base .

3.- I call the wordCalculator function (here I calculate the new number), this function has 3 arguments (word, number and base), with the prototype of the function you can see that I use the pointer to the word as a real argument (because I will edit the array values )

3.1.- Inside this function (it can be a class method in java), I declare 3 new local variables: result (which takes the value of the number), i (counter) and difference (this will be used in the operation), but how the conversion works ?

 /* Example: 125(10) -----> ?(2) 125 |_2 -1- 62 |_2 -0- 31 |_2 -1- 15 |_2 -1- 7 |_2 -1- 3 |_2 -1- 1 */ 

So, in this example, the binary number for 125 (10) is 1111101 (2), and this is the process that I describe in my function.

3.2.- I calculated% of the whole division and saved it in difference , then divided the result into base (its numerical copy) and loaded it into (this is the link to the next step), after this operation I save the difference in the first location of the word of the array [] (to which the pointer *(word + i) refers *(word + i) , i represents the offset), if the result gives a number below the base, the operation is completed, and the last digit is stored in the array (this is if ( result < base ) ) ..

 /* Functions declaration (Prototype) */ int wordCalculator( int * const word, long int number, int base ); int main( void ) { int i, base; int word[ 32 ]; unsigned long int number; printf( "Enter the decimal number to be converted: " ); scanf( "%ld", &number ); printf( "\nEnter the new base: " ); scanf( "%d", &base ); i = wordCalculator( word, number, base ); printf( "The number is: " ); for(; i >= 0; i--){ if ( word[ i ] <= 9) printf( "%d", word[ i ] ); else /* 65 represents A in ASCII code. */ printf( "%c", ( 65 - 10 + word[ i ] ) ); } printf( "\n" ); } int wordCalculator( int * const word, long int number, int base ) { unsigned long int result = number; int i, difference; i = 0; do{ difference = result % base; result /= base; *( word + i ) = difference; i++; if ( result < base ) *( word + i ) = result; } while( result >= base ); return i; } 
+1
source

Source: https://habr.com/ru/post/1443099/


All Articles