遇到一个问题,需要输出函数的bytebuffer参数数据,用到了下面的代码:
ByteBuffer buf = (ByteBuffer) param.args[0];
byte[] rawBuf = new byte[buf.limit()];
while(buf.hasRemaining()){
buf.get(rawBuf);
}
上面这段可以将object对象转换为 ByteBuffer 变量 和 bytearray 变量。
获取byte[]类型的参数可以使用下面代码:
byte[] data = (byte[]) param.args[0];
然后就可以进行相应的操作,如打印了。
打印byte数组可以使用下面的函数:
class HexDumper
{
private final static char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' };
// 将字节数组的数据转换为16进制的字符串数据
public static String dumpHexString(byte[] array)
{
if (array.equals(null))
return "null";
// 申请内存空间
byte[] byte2 = new byte[array.length + 0x10];
// 内存空间清零
for(int i = 0; i < byte2.length; i++)
{
byte2[i] = 0;
}
// 将传入的字节数组中的数据拷贝到新数组中
for(int i = 0; i < array.length; i++)
{
byte2[i] = array[i];
}
// 将字节数组数据转换为16进制的字符串数据
return dumpHexString(byte2, 0, byte2.length);
}
// 将字节数组数据转换为16进制的字符串数据
public static String dumpHexString(byte[] array, int offset, int length)
{
StringBuilder result = new StringBuilder();
byte[] line = new byte[16];
int lineIndex = 0;
// 打印数据的偏移
result.append("\n0x");
// 将整型字节数组偏移转换为16进制字符串数据
result.append(toHexString(offset));
for (int i = offset; i < offset + length; i++)
{
if (lineIndex == 16)
{
result.append(" ");
for (int j = 0 ; j < 16 ; j++)
{
if (line[j] > ' ' && line[j] < '~')
{
result.append(new String(line, j, 1));
}
else
{
result.append(".");
}
}
// 打印数据的偏移
result.append("\n0x");
result.append(toHexString(i));
lineIndex = 0;
}
byte b = array[i];
result.append(" ");
result.append(HEX_DIGITS[(b >>> 4) & 0x0F]);
result.append(HEX_DIGITS[b & 0x0F]);
line[lineIndex++] = b;
}
if (lineIndex != 16)
{
int count = (16 - lineIndex) * 3;
count++;
for (int i = 0 ; i < count ; i++)
{
result.append(" ");
}
for (int i = 0 ; i < lineIndex ; i++)
{
if (line[i] > ' ' && line[i] < '~')
{
result.append(new String(line, i, 1));
}
else
{
result.append(".");
}
}
}
return result.toString();
}
// 将单字节数组转化为16进制的字符串
public static String toHexString(byte b)
{
// 将字节数组转换为16进制的字符串数据
return toHexString(toByteArray(b));
}
// 将字节数组转换为16进制的字符串数据
public static String toHexString(byte[] array)
{
// 将指定字节数组的指定偏移位置指定长度的字节数据转换为16进制字符串进行显示
return toHexString(array, 0, array.length);
}
// 将指定字节数组的指定偏移位置指定长度的字节数据转换为16进制字符串进行显示
public static String toHexString(byte[] array, int offset, int length)
{
// 申请内存空间存放字符数组
char[] buf = new char[length * 2];
int bufIndex = 0;
for (int i = offset ; i < offset + length; i++)
{
// 取传入数组中的1字节数据
byte b = array[i];
// 取字节数据中高4位的数据转换为16进制字符串
buf[bufIndex++] = HEX_DIGITS[(b >>> 4) & 0x0F];
// 取字节数据中低4位的数据转换为16进制字符串
buf[bufIndex++] = HEX_DIGITS[b & 0x0F];
}
// 返回最终转换成功的字符串
return new String(buf);
}
// 将int整型数组转换为16进制的字符串进行显示
public static String toHexString(int i)
{
// 将字节数组转换为16进制的字符串数据
return toHexString(toByteArray(i));
}
// 将单字节数组转化为字节数组进行存储
public static byte[] toByteArray(byte b)
{
byte[] array = new byte[1];
array[0] = b;
return array;
}
// 将int整型数转换为4字节的字节数组
public static byte[] toByteArray(int i)
{
byte[] array = new byte[4];
array[3] = (byte)(i & 0xFF);
array[2] = (byte)((i >> 8) & 0xFF);
array[1] = (byte)((i >> 16) & 0xFF);
array[0] = (byte)((i >> 24) & 0xFF);
return array;
}
// 将单个字符转换为整型
private static int toByte(char c)
{
if (c >= '0' && c <= '9') return (c - '0');
if (c >= 'A' && c <= 'F') return (c - 'A' + 10);
if (c >= 'a' && c <= 'f') return (c - 'a' + 10);
throw new RuntimeException ("Invalid hex char '" + c + "'");
}
// 将字符串数据转换为相应的字节数组数据进行存储
public static byte[] hexStringToByteArray(String hexString)
{
int length = hexString.length();
byte[] buffer = new byte[length / 2];
// 每次处理2个字符的字符串
for (int i = 0 ; i < length ; i += 2)
{
// 例如将"23"转换为0x23
buffer[i / 2] = (byte)((toByte(hexString.charAt(i)) << 4) | toByte(hexString.charAt(i+1)));
}
return buffer;
}
}