HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Binary-octal-hexadecimal conversion table

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
conversionhexadecimalbinaryoctaltable

Problem

I solved this exercise of displaying a table of numbers from 1 to 256 in binary, octal and hexadecimal. I made this program to convert from decimal to binary and took that binary number to convert to octal and hexadecimal to made a table of binary, octal and hexadecimal. You might see that the way I found out which was the binary representation of the decimal number can be applied to the other bases and I didn't do it that way because I wanted to see if I could do it like this, I'll try to apply that method to the other basis now. Note: I overloaded the method binToBase () because I just learnt how to do it this week and seemed like the perfect oportunity to test it, haha.

is this good code? I'm pretty ashamed of how long it took for me to solve this ( 2 days )

```
public class Table {

public static final int BIN_BASE = 2 ;
public static final String HEX_BASE = "Hexa" ;
public static final int OCT_BASE = 8 ;

public static final int ROOF = 256 ;

public static void main ( String[] args ) {

int bin ;

System.out.println ( "Binary\t\tOctal\t\tHexadecimal" );

for ( int i = 1 ; i 0 ) ;

return hex ;
}

public static int convertToBase ( int convert , int base ){

int factor = 1 ;
int bin = convert ;
int oct = 0 ;

do {

switch ( bin % 1000 ) {

case 0 :
break ;
case 1 :
oct += factor * 1 ;
break ;
case 10 :
oct += factor * 2 ;
break ;
case 11 :
oct += factor * 3 ;
break ;
case 100 :
oct += factor * 4 ;
break ;
case 101 :
oct += factor * 5 ;
break ;
case 110 :
oct += factor * 6 ;
break ;

Solution

You should use the same format for both the headers and the contents of the table.

It pays to read the documentation. The task could be accomplished very simply.

public static void main(String[] args) {
    String tableFmt = " %11s %11s %11s\n";
    System.out.printf(tableFmt, "Binary", "Octal", "Hexadecimal");
    for (int i = 1; i <= 256; i++) {
        System.out.printf(tableFmt, Integer.toBinaryString(i),
                                    Integer.toOctalString(i),
                                    Integer.toHexString(i).toUpperCase());
    }
}


Your choice of the base-two representation using an int rather than a String (using one hundred one to represent five) is very unconventional and not recommended.

You have also abused method overloading. The base parameter is unused. Rather, you are relying on just the type of the second parameter to pick which method to call.

Code Snippets

public static void main(String[] args) {
    String tableFmt = " %11s %11s %11s\n";
    System.out.printf(tableFmt, "Binary", "Octal", "Hexadecimal");
    for (int i = 1; i <= 256; i++) {
        System.out.printf(tableFmt, Integer.toBinaryString(i),
                                    Integer.toOctalString(i),
                                    Integer.toHexString(i).toUpperCase());
    }
}

Context

StackExchange Code Review Q#132363, answer score: 2

Revisions (0)

No revisions yet.