Moved format byte to string function
This commit is contained in:
parent
ea206cc5b5
commit
e28c84efb3
5 changed files with 80 additions and 76 deletions
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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>();
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -74,4 +74,29 @@ public class ByteUtilTest {
|
|||
assertArrayEquals(new byte[]{0x22,0x11}, ByteUtil.getReverseByteOrder(new byte[]{0x11,0x22}));
|
||||
assertArrayEquals(new byte[]{0x44,0x33,0x22,0x11}, ByteUtil.getReverseByteOrder(new byte[]{0x11,0x22,0x33,0x44}));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void toFormattedStringTest(){
|
||||
byte[] data = new byte[1];
|
||||
assertEquals("000 00 '. '",
|
||||
ByteUtil.toFormattedString(data));
|
||||
|
||||
data[0] = 65;
|
||||
assertEquals("000 41 'A '",
|
||||
ByteUtil.toFormattedString(data));
|
||||
|
||||
byte[] data2 = new byte[8];
|
||||
data2[4] = 65;
|
||||
assertEquals("000 00 00 00 00 41 00 00 00 '....A...'",
|
||||
ByteUtil.toFormattedString(data2));
|
||||
|
||||
byte[] data3 = new byte[32];
|
||||
data3[4] = 65;
|
||||
assertEquals("000 00 00 00 00 41 00 00 00 '....A...'\n"+
|
||||
"008 00 00 00 00 00 00 00 00 '........'\n"+
|
||||
"016 00 00 00 00 00 00 00 00 '........'\n"+
|
||||
"024 00 00 00 00 00 00 00 00 '........'",
|
||||
ByteUtil.toFormattedString(data3));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,6 @@
|
|||
package zutil;
|
||||
|
||||
import org.junit.Test;
|
||||
import zutil.StringUtil;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
|
|
@ -71,30 +70,6 @@ public class StringUtilTest {
|
|||
assertEquals( "aa", StringUtil.trimQuotes("\"aa\"") );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatBytesToStringTest(){
|
||||
byte[] data = new byte[1];
|
||||
assertEquals("000 00 '. '",
|
||||
StringUtil.formatBytesToString(data));
|
||||
|
||||
data[0] = 65;
|
||||
assertEquals("000 41 'A '",
|
||||
StringUtil.formatBytesToString(data));
|
||||
|
||||
byte[] data2 = new byte[8];
|
||||
data2[4] = 65;
|
||||
assertEquals("000 00 00 00 00 41 00 00 00 '....A...'",
|
||||
StringUtil.formatBytesToString(data2));
|
||||
|
||||
byte[] data3 = new byte[32];
|
||||
data3[4] = 65;
|
||||
assertEquals("000 00 00 00 00 41 00 00 00 '....A...'\n"+
|
||||
"008 00 00 00 00 00 00 00 00 '........'\n"+
|
||||
"016 00 00 00 00 00 00 00 00 '........'\n"+
|
||||
"024 00 00 00 00 00 00 00 00 '........'",
|
||||
StringUtil.formatBytesToString(data3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void joinTest(){
|
||||
assertEquals("", StringUtil.join(Arrays.asList(), ","));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue