public final static String byteArrayAsHex ( final byte [ ] buf , final int limit ) { final StringBuilder sb = new StringBuilder ( ) ; for ( int i = 0 ; i < limit ; ++ i ) { if ( ( i % 16 ) == 0 ) { // print offset sb . append ( nativeAsHex ( i , 32 ) ) . append ( " " ) ; } else if ( ( ( i ) % 8 ) == 0 ) { // split on qword sb . append ( " " ) ; } sb . append ( nativeAsHex ( ( buf [ i ] & 0xFF ) , 8 ) ) . append ( " " ) ; // hex byte if ( ( ( i % 16 ) == 15 ) || ( i == ( buf . length - 1 ) ) ) { for ( int j = ( 16 - ( i % 16 ) ) ; j > 1 ; j -- ) { // padding non exist bytes sb . append ( " " ) ; } sb . append ( " |" ) ; // byte columns final int start = ( ( i / 16 ) * 16 ) ; final int end = ( ( buf . length < i + 1 ) ? buf . length : ( i + 1 ) ) ; for ( int j = start ; j < end ; ++ j ) { if ( ( buf [ j ] >= 32 ) && ( buf [ j ] <= 126 ) ) { sb . append ( ( char ) buf [ j ] ) ; } else { sb . append ( "." ) ; // non-printable character } } sb . append ( "|\n" ) ; // end column } } return sb . toString ( ) ; }
Return hexdump of byte - array