在我们网络传输的过程中,底层都是字节数据的传输,
但是,我们若是想要获知字节的内容,却无法通过直接输出获得
因此,在本篇博文中,本人来给出一个小工具,来方便我们直接读取字节数据的内容
字节数据 与 字符串 的 相互转换:
字节数据 --> 字符串:
public static final String HEX = "0123456789ABCDEF";
public static String binaryToString(byte[] buffer, int offset, int len) {
StringBuffer result = new StringBuffer();
for (int index = offset; index < offset + len; index++) {
byte val = buffer[index];
result.append(HEX.charAt((val >> 4) & 0x0F)); // 一个字节 有 8位(即:两个16进制) 构成
result.append(HEX.charAt(val & 0x0F));
}
return result.toString();
}
那么,接下来是 将字符串转换为字节数据的代码:
字符串 --> 字节数据:
public static final String HEX = "0123456789ABCDEF";
public static byte[] stringToBinary(String str) {
if (str == null) {
return null;
}
int len = str.length();
if (len <= 0 || len % 2 != 0) {
return null;
}
byte[] result = new byte[len];
int binaryIndex = 0;
int strIndex = 0;
while (strIndex < len) {
int hVal = HEX.indexOf(str.charAt(strIndex));
int lVal = HEX.indexOf(str.charAt(strIndex + 1));
result[binaryIndex++] = (byte) ((hVal << 4) | lVal);
strIndex += 2;
}
return result;
}
接下来,本人来讲解下 将int数据与字节数据的相互转换:
字节数据 与 int数据 的 相互转换:
字节数据 --> int数据:
// 从指定位置开始,将字节数组转化为int数据(高低低高顺序)
public static int bytesToInt(byte[] bytes, int offset) {
int res = 0;
for (int index = 3; index >= 0; index--) {
res <<= 8;
res |= bytes[offset + index] & 0xFF; //这里的代码在下文中 解释
}
return res;
}
// 将整个字节数组都转化为int数据(高低低高顺序)
public static int bytesToInt(byte[] bytes) {
int res = 0;
for (int index = 3; index >= 0; index--) {
res <<= 8;
res |= 0xFF & bytes[index]; //这里的代码在下文中 解释
}
return res;
}
在这里,本人对上面代码中的注释的代码给出解释:
为什么要 & 0xFF 呢?
答曰:
无论是long还是int,在Java中都是以补码形式存在的
当 |= byte类型的数据时,会自动补给高位补1
就导致我们转换过去的byte类型的量是 -|转换前的数据| 的值
因此,要 & 0xFF ,保证后两位数据有效,其它位数据均为0
int数据 --> 字节数据:
// 将整个int数据转换为byte数组
public static byte[] intToBytes(int val) {
byte[] res = new byte[4];
for (int index = 0; index < res.length; index++) {
res[index] = (byte) ((val >> (index * 8)) & 0xFF);
}
return res;
}
// 从指定下标开始,将整个int数据转换为 目标byte数组的偏移量的后续内容
public static byte[] intToBytes(byte[] res, int offset, int val) {
byte intBytes[] = intToBytes(val);
for (int index = 0; index < intBytes.length; index++) {
res[offset + index] = intBytes[index];
}
return res;
}
接下来,本人来讲解下 将int数据与字节数据的相互转换:
字节数据 与 long数据 的 相互转换:
字节数据 --> long数据数据:
// 从指定位置开始,将字节数组转化为long数据(高低低高顺序)
public static long bytesToLong(byte[] bytes, int offset) {
long res = 0;
for (int index = 7; index >= 0; index--) {
res <<= 8;
res |= bytes[offset + index] & 0xFF;
}
return res;
}
// 将整个字节数组都转化为long数据(高低低高顺序)
public static long bytesToLong(byte[] bytes) {
long res = 0;
for (int index = 7; index >= 0; index--) {
res <<= 8;
res |= bytes[index] & 0xFF;
}
return res;
}
long数据 --> 字节数据:
// 将整个long数据转换为byte数组
public static byte[] longToBytes(long val) {
byte[] res = new byte[8];
for (int index = 0; index < res.length; index++) {
res[index] = (byte) ((val >> (index * 8)) & 0xFF);
}
return res;
}
// 从指定下标开始,将整个long数据转换为 目标byte数组的偏移量的后续内容
public static byte[] longToBytes(byte[] res, int offset, long val) {
byte intBytes[] = longToBytes(val);
for (int index = 0; index < intBytes.length; index++) {
res[offset + index] = intBytes[index];
}
return res;
}
最后,本人将上述两个方法整合,放在一个 工具类中:
小工具 —— 字节数据 的 解析与转换:
package edu.youzg.util;
public class YouzgBinary {
public static final String HEX = "0123456789ABCDEF";
public YouzgBinary() {
}
public static byte[] stringToBinary(String str) {
if (str == null) {
return null;
}
int len = str.length();
if (len <= 0 || len % 2 != 0) {
return null;
}
byte[] result = new byte[len];
int binaryIndex = 0;
int strIndex = 0;
while (strIndex < len) {
int hVal = HEX.indexOf(str.charAt(strIndex));
int lVal = HEX.indexOf(str.charAt(strIndex + 1));
result[binaryIndex++] = (byte) ((hVal << 4) | lVal);
strIndex += 2;
}
return result;
}
public static String binaryToString(byte[] buffer, int offset, int len) {
StringBuffer result = new StringBuffer();
for (int index = offset; index < offset + len; index++) {
byte val = buffer[index];
result.append(HEX.charAt((val >> 4) & 0x0F));
result.append(HEX.charAt(val & 0x0F));
}
return result.toString();
}
}
对于这种转换,我们也可以用于信息的加密
至于应用,本人将在之后的博文中进行展示