4
\$\begingroup\$

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 <= ROOF ; i++ ){
            bin = convertToBin ( i ) ;
            System.out.printf ( "%10d\t" , bin ) ;
            System.out.printf ( "%10s\t" , convertToBase ( bin , OCT_BASE ) ) ;
            System.out.printf ( "%10s\n" , convertToBase ( bin, HEX_BASE ) ) ;
        }

    }

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

        String hex = "" ;
        int bin = convert ;

        do {
            switch ( bin % 10000 ) {

                case 0 :
                    hex = '0' + hex ;
                    break ;
                case 1 :
                    hex = '1' + hex ;
                    break ;
                case 10 :
                    hex = '2' + hex ;
                    break ;
                case 11 :
                    hex = '3' + hex ;
                    break ;
                case 100 :
                    hex = '4' + hex ;
                    break ;
                case 101 :
                    hex = '5' + hex ;
                    break ;
                case 110 :
                    hex = '6' + hex ;
                    break ;
                case 111 :
                    hex = '7' + hex ;
                    break ;
                case 1000 :
                    hex = '8' + hex ;
                    break ;
                case 1001 :
                    hex = '9' + hex ;
                    break ;
                case 1010 :
                    hex = 'A' + hex ;
                    break ;
                case 1011 :
                    hex = 'B' + hex ;
                    break ;
                case 1100 :
                    hex = 'C' + hex ;
                    break ;
                case 1101 :
                    hex = 'D' + hex ;
                    break ;
                case 1110 :
                    hex = 'E' + hex ;
                    break ;
                default :
                    hex = 'F' + hex ;
                    break ;

            }

            bin /= 10000 ;

        } while ( bin > 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 ;
                default :
                    oct += factor * 7 ;
                    break ;

            }

            bin /= 1000 ;

            factor *= 10 ;

        } while ( bin > 0 ) ;

        return oct ;

    }

    public static int convertToBin ( int original ) {

        int bin = 0 ;
        int sum = 0 ;
        int exponent = 0 ;

        while ( sum < original ){

            sum += Math.pow ( 2 , exponent ) ;
            exponent ++ ;
            bin *= 10 ;
            bin ++ ;

        }

        while ( sum > original && exponent >= 0 ){

            if ( sum - Math.pow ( 2 , exponent ) >=  original ) {

                sum -= Math.pow ( 2 , exponent ) ;
                bin -= Math.pow ( 10 , exponent ) ;

            }

            exponent-- ;

        }

        return bin ;

    }

}

And this is the output

Binary      Octal       Hexadecimal
         1           1           1
        10           2           2
        11           3           3
       100           4           4
       101           5           5
       110           6           6
       111           7           7
      1000          10           8
      1001          11           9
      1010          12           A
      1011          13           B
      1100          14           C
      1101          15           D
      1110          16           E
      1111          17           F
     10000          20          10
     10001          21          11
     10010          22          12
     10011          23          13
     10100          24          14
     10101          25          15
     10110          26          16
     10111          27          17
     11000          30          18
     11001          31          19
     11010          32          1A
     11011          33          1B
     11100          34          1C
     11101          35          1D
     11110          36          1E
     11111          37          1F
    100000          40          20
    100001          41          21
    100010          42          22
    100011          43          23
    100100          44          24
    100101          45          25
    100110          46          26
    100111          47          27
    101000          50          28
    101001          51          29
    101010          52          2A
    101011          53          2B
    101100          54          2C
    101101          55          2D
    101110          56          2E
    101111          57          2F
    110000          60          30
    110001          61          31
    110010          62          32
    110011          63          33
    110100          64          34
    110101          65          35
    110110          66          36
    110111          67          37
    111000          70          38
    111001          71          39
    111010          72          3A
    111011          73          3B
    111100          74          3C
    111101          75          3D
    111110          76          3E
    111111          77          3F
   1000000         100          40
   1000001         101          41
   1000010         102          42
   1000011         103          43
   1000100         104          44
   1000101         105          45
   1000110         106          46
   1000111         107          47
   1001000         110          48
   1001001         111          49
   1001010         112          4A
   1001011         113          4B
   1001100         114          4C
   1001101         115          4D
   1001110         116          4E
   1001111         117          4F
   1010000         120          50
   1010001         121          51
   1010010         122          52
   1010011         123          53
   1010100         124          54
   1010101         125          55
   1010110         126          56
   1010111         127          57
   1011000         130          58
   1011001         131          59
   1011010         132          5A
   1011011         133          5B
   1011100         134          5C
   1011101         135          5D
   1011110         136          5E
   1011111         137          5F
   1100000         140          60
   1100001         141          61
   1100010         142          62
   1100011         143          63
   1100100         144          64
   1100101         145          65
   1100110         146          66
   1100111         147          67
   1101000         150          68
   1101001         151          69
   1101010         152          6A
   1101011         153          6B
   1101100         154          6C
   1101101         155          6D
   1101110         156          6E
   1101111         157          6F
   1110000         160          70
   1110001         161          71
   1110010         162          72
   1110011         163          73
   1110100         164          74
   1110101         165          75
   1110110         166          76
   1110111         167          77
   1111000         170          78
   1111001         171          79
   1111010         172          7A
   1111011         173          7B
   1111100         174          7C
   1111101         175          7D
   1111110         176          7E
   1111111         177          7F
  10000000         200          80
  10000001         201          81
  10000010         202          82
  10000011         203          83
  10000100         204          84
  10000101         205          85
  10000110         206          86
  10000111         207          87
  10001000         210          88
  10001001         211          89
  10001010         212          8A
  10001011         213          8B
  10001100         214          8C
  10001101         215          8D
  10001110         216          8E
  10001111         217          8F
  10010000         220          90
  10010001         221          91
  10010010         222          92
  10010011         223          93
  10010100         224          94
  10010101         225          95
  10010110         226          96
  10010111         227          97
  10011000         230          98
  10011001         231          99
  10011010         232          9A
  10011011         233          9B
  10011100         234          9C
  10011101         235          9D
  10011110         236          9E
  10011111         237          9F
  10100000         240          A0
  10100001         241          A1
  10100010         242          A2
  10100011         243          A3
  10100100         244          A4
  10100101         245          A5
  10100110         246          A6
  10100111         247          A7
  10101000         250          A8
  10101001         251          A9
  10101010         252          AA
  10101011         253          AB
  10101100         254          AC
  10101101         255          AD
  10101110         256          AE
  10101111         257          AF
  10110000         260          B0
  10110001         261          B1
  10110010         262          B2
  10110011         263          B3
  10110100         264          B4
  10110101         265          B5
  10110110         266          B6
  10110111         267          B7
  10111000         270          B8
  10111001         271          B9
  10111010         272          BA
  10111011         273          BB
  10111100         274          BC
  10111101         275          BD
  10111110         276          BE
  10111111         277          BF
  11000000         300          C0
  11000001         301          C1
  11000010         302          C2
  11000011         303          C3
  11000100         304          C4
  11000101         305          C5
  11000110         306          C6
  11000111         307          C7
  11001000         310          C8
  11001001         311          C9
  11001010         312          CA
  11001011         313          CB
  11001100         314          CC
  11001101         315          CD
  11001110         316          CE
  11001111         317          CF
  11010000         320          D0
  11010001         321          D1
  11010010         322          D2
  11010011         323          D3
  11010100         324          D4
  11010101         325          D5
  11010110         326          D6
  11010111         327          D7
  11011000         330          D8
  11011001         331          D9
  11011010         332          DA
  11011011         333          DB
  11011100         334          DC
  11011101         335          DD
  11011110         336          DE
  11011111         337          DF
  11100000         340          E0
  11100001         341          E1
  11100010         342          E2
  11100011         343          E3
  11100100         344          E4
  11100101         345          E5
  11100110         346          E6
  11100111         347          E7
  11101000         350          E8
  11101001         351          E9
  11101010         352          EA
  11101011         353          EB
  11101100         354          EC
  11101101         355          ED
  11101110         356          EE
  11101111         357          EF
  11110000         360          F0
  11110001         361          F1
  11110010         362          F2
  11110011         363          F3
  11110100         364          F4
  11110101         365          F5
  11110110         366          F6
  11110111         367          F7
  11111000         370          F8
  11111001         371          F9
  11111010         372          FA
  11111011         373          FB
  11111100         374          FC
  11111101         375          FD
  11111110         376          FE
  11111111         377          FF
 100000000         400         100
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ thank you so much! that was a beutiful answer ... I knew about the base parameter, as I said, I only used it because I learnt about method overloading this week and just wanted to try it out, but I will definitly keep that in mind, even though I know, that wasn't an elegant way... The part about format and the use of binary base as an integer, were just very useful! Thanks again! \$\endgroup\$ – newbie Jun 18 '16 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.