Encoding a character into HammingCode in Java





5
Date Submitted Tue. Oct. 10th, 2006 9:43 AM
Revision 1 of 1
Helper fastmike
Tags codes
Comments 0 comments
First of all it is very necessary to know what hammingcode is. my program will convert any char to ASCII(Binary) and then convert it into CheckBits(HammingCode). to compile this program type javac printBits2.java and then java printBits2 H( or any other char you would like to convert). i know i had alot of hard time figuring but java forums help me out alot.

public class printBits2{
        public static void main( String args[] ){
                printBits2 pb = new printBits2( args[ 0 ] );
        }

        public printBits2( String message ){
                //System.out.print( message + "\n\n" );
                char chA[] = new char[ message.length() ];
                chA = message.toCharArray();
                System.out.print( chA[ 0 ] + " -> is your input char\n" );
                printCharBits( chA[ 0 ] );
                System.out.print( " -> is what computer sees\n\n" );
                toHamming( chA[ 0 ] );
        }

        private void toHamming( char c ){
                char displayMask = 1 << 0;
                char n[]; n = new char[ 4 ]; n[ 0 ] = 91; n[ 1 ] = 109; n[ 2 ] = 7; n[ 3 ] = 7;
                int j = 0;
                boolean b = true;
                char HC = 0 << 15;
                for( int i = 16; i > 5; i-- ){
                        if( 16 % ( 17 - i ) == 0 ){
                                //insert the appropriate parity bits
                                n[ j ] = (char) ( c & n[ j ] );
                                for( int k = 0; k < 7; k++ ){
                                        if( (int) ( n[ j ] & displayMask ) == 1 ){ b = !b; }
                                        n[ j ] >>=1;
                                }
                                if( ! b ){
                                        int helpMask = 1 << (int) ( Math.pow( 2, j ) - 1 );
                                        HC = (char) ( helpMask | (int) HC );
                                }
                                j++;
                                b = true;
                        }
                        else{
                                if( ( c & displayMask ) == displayMask ){
                                        int helpMask = 1 << ( 16 - i );
                                        HC = (char) ( helpMask | (int) HC );
                                }
                                c >>=1;
                        }
                }
                printCharBits( HC ); System.out.print( " -> hamming code computer sees\n\n" );
        }

        private void printCharBits( char c ){
                char displayMask = 1 << 15;
                for ( int bit = 1; bit < 17; bit++ ){
                        if( ( c & displayMask ) == 0 ){
                                System.out.print( "0" );
                                if( bit % 8 == 0 ){ System.out.print( " " ); }
                        }
                        else{
                                System.out.print( "1" );
                                if( bit % 8 == 0 ){ System.out.print( " " ); }
                        }
                        c <<= 1;
                }
        }
}

 

chris mike

Comments

There are currently no comments for this snippet.

Voting

Votes Down