private static String convertByteArrayToString ( byte [ ] byteArray ) { if ( byteArray == null ) { throw new IllegalArgumentException ( "Byte array must not be null!" ) ; } StringBuffer hexString = new StringBuffer ( ) ; for ( int i = 0 ; i < byteArray . length ; i ++ ) { int digit = 0xFF & byteArray [ i ] ; String hexDigits = Integer . toHexString ( digit ) ; if ( hexDigits . length ( ) < 2 ) { hexString . append ( "0" ) ; } hexString . append ( hexDigits ) ; } return hexString . toString ( ) ; }
This method converts a byte array into a string converting each byte into a 2 - digit hex representation and appending them all together .