Moved format byte to string function

This commit is contained in:
Ziver Koc 2016-04-26 14:24:16 +02:00
parent ea206cc5b5
commit e28c84efb3
5 changed files with 80 additions and 76 deletions

View file

@ -24,6 +24,8 @@
package zutil;
import zutil.converter.Converter;
/**
* Utility functions for byte primitive type
*
@ -124,4 +126,54 @@ public class ByteUtil {
throw new IllegalArgumentException("Invalid length argument: "+length+", allowed values: 1 to "+(index+1)+" for index "+index);
return (byte) BYTE_MASK[index][length];
}
/**
* Presents a binary array in HEX and ASCII
*
* @param data The source binary data to format
* @return A multiline String with human readable HEX and ASCII
*/
public static String toFormattedString(byte[] data){
StringBuffer output = new StringBuffer();
//000 XX XX XX XX XX XX XX XX '........'
int maxOffset = (""+data.length).length();
for(int offset=0; offset<data.length; offset+=8){
if(offset != 0)
output.append('\n');
// Offset
String offsetStr = ""+offset;
for(int i=offsetStr.length(); i<3 || i<maxOffset; ++i){
output.append('0');
}
output.append(offsetStr);
output.append(" ");
// HEX
for(int i=0; i<8; ++i){
if(offset+i < data.length)
output.append(Converter.toHexString(data[offset+i]));
else
output.append(" ");
output.append(' ');
}
output.append(' ');
// ACII
output.append('\'');
for(int i=0; i<8; ++i){
if(offset+i < data.length)
if( 32 <= data[offset+i] && data[offset+i] <= 126 )
output.append((char)data[offset+i]);
else
output.append('.');
else
output.append(' ');
}
output.append('\'');
}
return output.toString();
}
}

View file

@ -195,56 +195,6 @@ public class StringUtil {
return str;
}
/**
* Presents a binary array in HEX and ASCII
*
* @param data The source binary data to format
* @return A multiline String with human readable HEX and ASCII
*/
public static String formatBytesToString(byte[] data){
StringBuffer output = new StringBuffer();
//000 XX XX XX XX XX XX XX XX '........'
int maxOffset = (""+data.length).length();
for(int offset=0; offset<data.length; offset+=8){
if(offset != 0)
output.append('\n');
// Offset
String offsetStr = ""+offset;
for(int i=offsetStr.length(); i<3 || i<maxOffset; ++i){
output.append('0');
}
output.append(offsetStr);
output.append(" ");
// HEX
for(int i=0; i<8; ++i){
if(offset+i < data.length)
output.append(Converter.toHexString(data[offset+i]));
else
output.append(" ");
output.append(' ');
}
output.append(' ');
// ACII
output.append('\'');
for(int i=0; i<8; ++i){
if(offset+i < data.length)
if( 32 <= data[offset+i] && data[offset+i] <= 126 )
output.append((char)data[offset+i]);
else
output.append('.');
else
output.append(' ');
}
output.append('\'');
}
return output.toString();
}
private static ArrayList<String> SPACES = new ArrayList<String>();
/**

View file

@ -73,7 +73,9 @@ public class Converter {
*/
public static byte[] toBytes(char[] arr){
byte[] ret = new byte[arr.length];
System.arraycopy(arr, 0, ret, 0, arr.length);
for (int i=0; i<arr.length; ++i)
ret[i] = (byte) (arr[i] & 0xFF);
//System.arraycopy(arr, 0, ret, 0, arr.length); // does not work if char value is largen than 128
return ret;
}