byte数组和String之间的相互转换代码

本文介绍了一种将字符串转换为字节并进行压缩的方法,同时也提供了将压缩后的字节数据转换回原始字符串的实现。该方法使用了GZIP压缩算法,并通过字节数组进行数据传递。
public static String converByteToString(byte[] data) {
		ByteArrayInputStream byteInput = null;
		GZIPInputStream gzin = null;
		ByteArrayOutputStream byteOutput = null;
		String data = null;
		byte[] byteData = null;
		byte[] buf = new byte[1024]; 
		try {
			byteInput = new ByteArrayInputStream(zippedData);
			gzin = new GZIPInputStream(byteInput);
			byteOutput = new ByteArrayOutputStream();
			int num = -1;
			while ((num = gzin.read(buf, 0, buf.length)) != -1) {
				byteOutput.write(buf, 0, num);
				byteOutput.flush();
			}
			byteData = byteOutput.toByteArray();
			if (null != byteOutput) {
				byteOutput.close();
			}
			if (null != byteInput) {
				byteInput.close();
			}
			if (null != gzin) {
				gzin.close();
			}
			data = new String(byteData, "UTF-8");
			
		} catch (IOException e) {
			log.error("[CrabmanFileUtil.converZipToString] [{0}]",e.getMessage());
			return null;
		}finally{
			byteInput = null;
			gzin = null;
			byteOutput = null;
			buf = null;
			byteData = null;
		}
		return data;
	}
	
	public static byte[] converStringToByte(String str) {
		ByteArrayOutputStream byteOutput = null;
		GZIPOutputStream gzout = null;
		byte[] data = null;
		try {
			byteOutput = new ByteArrayOutputStream();
			gzout = new GZIPOutputStream(byteOutput);
			gzout.write(str.getBytes("UTF-8"));
			gzout.finish();
			data = byteOutput.toByteArray();
		} catch (IOException e) {
			log.error(e.getMessage());
			return null;
		} finally{
			try {
				if (gzout != null) {
					gzout.close();
				}
				if (byteOutput != null) {
					byteOutput.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
				gzout = null;
				byteOutput = null;
			}
		}
		
		return data;
	}

Java中byte数组String出现乱码问题,可通过以下方法解决: - **指定字符编码**:在转换过程中指定合适的字符编码,如使用"ISO-8859-1"编码可以解决部分乱码问题。示例代码如下: ```java byte[] byteArray = { /* 初始化字节数组 */ }; try { String str = new String(byteArray, "ISO-8859-1"); // 后续操作 } catch (java.io.UnsupportedEncodingException e) { e.printStackTrace(); } ``` 在将byte[]数组转成String,再将String转成byte[]数组时,使用该编码可避免因编码问题导致拿到的byte[]数组原来的byte[]数组不一致的情况[^1]。 - **确保编码表匹配**:通过`String.getBytes(String decode)`方法来得到byte[]时,要确定`decode`的编码表中确实存在String表示的码值,这样得到的byte[]数组才能正确被还原。例如: ```java String originalStr = "需要转换的字符串"; byte[] byteArray = originalStr.getBytes("UTF-8"); try { String newStr = new String(byteArray, "UTF-8"); // 后续操作 } catch (java.io.UnsupportedEncodingException e) { e.printStackTrace(); } ``` 若编码表不匹配,不同编码规范处理相同字节数据会导致乱码,如GB2312规范的中文在英文系统上按ASCII或Java的UNICODE规范处理就会出现乱码[^2][^4]。 - **去除填充的0**:当数据长度小于数组给定的长度时,数组剩余部分会自动填充0,直接String会导致乱码。可使用`StringUtils.byteToStr(bytes)`方法去掉自动填充的0。示例代码如下: ```java import org.apache.commons.lang3.StringUtils; // 指定长度100 byte[] bytes = new byte[100]; // 此处省略获取数据,假设byte[]中的数据实际长度为30,剩余的70将会自动填充0 String str1 = StringUtils.byteToStr(bytes); System.out.print(str1); // 正确数据,只有实际的30位,没有填充的乱码 ``` 这样能避免因填充的0导致页面展示乱码的问题[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Spring_java_gg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值