Skip to content

Instantly share code, notes, and snippets.

@Fuji1405116
Created November 2, 2022 06:50
Show Gist options
  • Save Fuji1405116/1539ec50bab39486ed6e71530fcc7e82 to your computer and use it in GitHub Desktop.
Save Fuji1405116/1539ec50bab39486ed6e71530fcc7e82 to your computer and use it in GitHub Desktop.
printing a Byte Array beautifully in Binary form
package media.Utils;
public class SimpleByteArrayPrinter {
/**
* print a byte array in binary form from offset index to offset+length index
* @param bytes byte array to be printed
* @param offset starting index of the byte array
* @param length length of the byte array you wanna print from starting index
* @param bytesInSingleLine how many binary formed bytes I want in single line
* @return a String containing bytes in binary form , fixed number of bytes in single line
*/
public static String printByteArray(byte[] bytes, int offset, int length,int spaceBetween, int bytesInSingleLine){
StringBuilder sb = new StringBuilder("");
sb.append("\n");
if(offset > bytes.length || (offset+length) > bytes.length ) return "'offset' or 'offset+length' is greater than byte array length .. !";
for(int i = offset ; i < offset+length ; i++){
for(int k = 7 ; k >= 0 ; k--){
int j = 1 << k;
j = bytes[i] & j;
j = j >> k;
sb.append(j);
}
if((i - offset + 1) % bytesInSingleLine == 0 ) sb.append("\n");
else {
for(int m=0; m<spaceBetween ; m++) {
sb.append(" ");
}
}
}
sb.append("\n");
return sb.toString();
}
/**
* print a byte array beautifully in binary form from offset index to offset+length index
* @param bytes byte array to be printed
* @param offset starting index of the byte array
* @param length length of the byte array you wanna print from starting index
* @param spaceBetween how many binary formed bytes I want in single line
* @param bytesInSingleLine how many binary formed bytes I want in single line
* @return a String containing bytes in binary form , fixed number of bytes in single line
*/
public static String printByteArrayBeautifully(byte[] bytes, int offset, int length, int spaceBetween, int bytesInSingleLine){
StringBuilder sb = new StringBuilder("");
sb.append("\n");
if(offset > bytes.length || (offset+length) > bytes.length ) return "'offset' or 'offset+length' is greater than byte array length .. !";
for(int l = 0; l < bytesInSingleLine ; l++){
sb.append(" 0 1 2 3 4 5 6 7");
for(int i=0;i<spaceBetween+1;i++){
sb.append(" ");
}
}
sb.append("\n");
for(int l = 0; l < bytesInSingleLine ; l++){
sb.append("+-+-+-+-+-+-+-+-+");
for(int i=0;i<spaceBetween;i++){
sb.append(" ");
}
}
sb.append("\n");
for(int i = offset ; i < offset+length ; i++){
if( (i - offset) != 0 && (i - offset) % bytesInSingleLine == 0 ){
for(int l = 0; l < bytesInSingleLine ; l++){
if(i+l+1 > offset+length) break;
sb.append(" 0 1 2 3 4 5 6 7");
for(int j=0;j<spaceBetween+1;j++){
sb.append(" ");
}
}
sb.append("\n");
for(int l = 0; l < bytesInSingleLine ; l++){
if(i+l+1 > offset+length) break;
sb.append("+-+-+-+-+-+-+-+-+");
for(int j=0;j<spaceBetween;j++){
sb.append(" ");
}
}
sb.append("\n");
}
sb.append("|");
for(int k = 7 ; k >= 0 ; k--){
int j = 1 << k;
j = bytes[i] & j;
j = j >> k;
sb.append(j);
sb.append("|");
}
if((i - offset + 1) % bytesInSingleLine == 0 || i+1 == offset+length){
sb.append("\n");
if(i+1 == offset+length && length % bytesInSingleLine != 0) bytesInSingleLine = length % bytesInSingleLine;
for(int l = 0; l < bytesInSingleLine ; l++){
sb.append("+-+-+-+-+-+-+-+-+");
for(int j=0;j<spaceBetween;j++){
sb.append(" ");
}
}
sb.append("\n");
sb.append("\n\n");
}
else{
for(int j=0;j<spaceBetween;j++){
sb.append(" ");
}
}
}
return sb.toString();
}
public static void main(String[] args) {
byte[] bytes = new byte[6];
for(int i = 0; i < bytes.length ; i++){
bytes[i] = (byte) i;
}
System.out.println("bytes = [ "+printByteArray(bytes,0,bytes.length,2,4)+" ]");
System.out.println();
System.out.println("bytes = [ "+printByteArrayBeautifully(bytes,0,bytes.length,2,4)+" ]");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment