java Binary_Octal_Hexidecimal.java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Binary_Octal_Hexidecimal.java相关的知识,希望对你有一定的参考价值。

public class BinaryOctalHexadecimalNumbers 
{
	    public static final int binary = 2;
	    public static final String HEXIDECIMAL = "Hexa";
	    public static final int OCTAL = 8;
	    public static final int TOP_VAL = 256;
	    
	public static void main(String[] args) 
	{
		
		
		int bin = 0 ;

        System.out.println ( "  Binary           Octal             Hex" );

        for ( int i = 1 ; i <= TOP_VAL ; i++ ){
            bin = binaryConversion ( i ) ;
            System.out.printf ( "%8d\t" , bin ) ;
            System.out.printf ( "%8s\t" , binary_OCT ( bin , OCTAL ) ) ;
            System.out.printf ( "%8s\n" , binary_hex ( bin, HEXIDECIMAL ) ) ;
        }

    }


    public static int binary_OCT ( int convert , int binary )
    {

        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 binaryConversion ( int Input ) {

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

        while ( sum < Input ){

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

        }

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

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

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

            }

            exponent-- ;

        }

        return bin ;

    }




public static String binary_hex ( int convert , String binary ) {

    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 ;
}
}

以上是关于java Binary_Octal_Hexidecimal.java的主要内容,如果未能解决你的问题,请参考以下文章

Java 布尔运算

java [Java] Java常用代码#java

Java - 35 Java 实例

Java While 循环

Java 字符串

Java If ... Else