【小工具】字节数据 的 解析与转换

在这里插入图片描述

在我们网络传输的过程中,底层都是字节数据的传输,
但是,我们若是想要获知字节的内容,却无法通过直接输出获得
因此,在本篇博文中,本人来给出一个小工具,来方便我们直接读取字节数据的内容

字节数据 与 字符串 的 相互转换:

字节数据 --> 字符串:

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();
	}
	
}

对于这种转换,我们也可以用于信息的加密
至于应用,本人将在之后的博文中进行展示


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在线字节转换float工具是一种方便的工具,可以将字节数据转换为浮点数。在计算机中,浮点数以二进制形式存储,并且使用特定的编码方法进行表示。字节数据转换为浮点数的过程就是将字节数据按照特定的编码规则解析成对应的浮点数。 这个工具可以通过输入字节数据,自动解析转换为浮点数。用户只需将需要转换字节数据输入到工具的输入框中,点击转换按钮,工具就会自动解析字节数据,并将其转换为浮点数。转换结果会以浮点数的形式显示在输出框中,方便用户进行查看和使用。 在线字节转换float工具不仅方便,而且还具有高度的准确性。它使用了标准的浮点数编码规则,确保了转换结果的正确性。同时,该工具还具有用户友好的界面设计,操作简单易懂,无需专业的计算机知识即可使用。 这个工具的应用场景非常广泛。在计算机领域,字节数据转换为浮点数常常用于数据解析和处理过程中。例如,在通信系统中,接收到的字节数据可能需要转换为浮点数进行进一步的计算和分析。使用这个工具,可以快速、准确地将字节数据转换为浮点数,方便后续的数据处理工作。 总之,在线字节转换float工具是一款实用的工具,能够快速、准确地将字节数据转换为浮点数。它具有方便、准确、简单易用等特点,适用于各种计算机领域的字节数据解析和处理工作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值