static final String toHex ( final byte [ ] input , final int len , final boolean upper ) { final char [ ] hex = new char [ len << 1 ] ; for ( int i = 0 , j = 0 ; i < len ; i ++ ) { final int bx = input [ i ] ; final int bh = ( ( bx >> 4 ) & 0xF ) ; final int bl = ( bx & 0xF ) ; if ( ( bh >= 0 ) && ( bh <= 9 ) ) { hex [ j ++ ] |= ( bh + ' ' ) ; } else if ( ( bh >= 0xA ) && ( bh <= 0xF ) ) { hex [ j ++ ] |= ( bh - 0xA + ( upper ? ' ' : ' ' ) ) ; } if ( ( bl >= 0x0 ) && ( bl <= 0x9 ) ) { hex [ j ++ ] |= ( bl + ' ' ) ; } else if ( ( bl >= 0xA ) && ( bl <= 0xF ) ) { hex [ j ++ ] |= ( bl - 0xA + ( upper ? ' ' : ' ' ) ) ; } } return new String ( hex ) ; }
Transform byte array to Hex String