public static String deWhite ( byte [ ] value ) { // If the value contains anything less than a space. StringBuilder buffer = new StringBuilder ( ) ; boolean bAllPrintable = true ; for ( byte b : value ) { if ( ( int ) ( b & 0xFF ) < ' ' ) { bAllPrintable = false ; break ; } } if ( bAllPrintable ) { // All >= space; convert directly to chars. for ( byte b : value ) { buffer . append ( ( char ) b ) ; } } else { // At least one non-printable. Convert to hex. buffer . append ( "0x" ) ; for ( byte b : value ) { buffer . append ( toHexChar ( ( ( int ) b & 0xF0 ) >> 4 ) ) ; buffer . append ( toHexChar ( ( ( int ) b & 0xF ) ) ) ; } } return buffer . toString ( ) ; }
Create a String by converting each byte directly into a character . However if any byte in the given value is less than a space a hex string is returned instead .