二进制和字符串之间的转换(包含一些小知识点运用)

package io.transformBinaryString;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


/**
 * 今天在网上看到一个程序游戏,主要目的是为了能够将文件中的字符串读取到程序,然后通过base64解码,再转换成二进制输出
 * 所以,自己写了个程序:(1)将一段字符串进行base64处理,然后转换成二进制输出。(2)将一段二进制数据转换成字符串,然后base64解码到对应的字符串
 * 
 */
public class TransformBS {
	/**
	* @see 字符串进行base64编码后转换为二进制形式,如:(h(原字符)->a(编码后)->01100001010000010011110100111101(二进制形式))
	*/
	@Test
	public void testS2B(){
		System.out.println("=========字符串到二进制!=============");
		BASE64Encoder e = new BASE64Encoder();//编码器
		String s = "hello World!";
		System.out.println("尚未编码的数据:"+s);
		s = e.encode(s.getBytes());//获得base64编码后的字符串
		System.out.println("编码后的数据:"+s);
		System.out.print("二进制数据:");
		for(char c:s.toCharArray()){//对字符串中的字符逐个转换成二进制数据
			String binaryStr = Integer.toBinaryString(c);//单个字符转换成的二进制字符串
			String format = String.format("%8s",binaryStr);//因为上面转换成二进制后的位数不够8位所以不足的前面补空格,这里是考虑到能够从数据文件批量读取。
			format =format.replace(" ","0");//高位空格替换成0,其实编码后的数据最大范围为2的6次方,首位一定是空格,不然就要用format.startWith(" ");来判断
			System.out.print(format);//输出
		}
		System.out.println("\n=========字符串到二进制结束!=============");
	}
	/**
	* @see 二进制形式转换为字符串后进行base64解码的字符串如:(01100001010000010011110100111101->a->h)
	*/
	@Test
	public void testB2S() throws IOException{
		System.out.println("=========二进制到字符串开始!=============");
		StringBuffer results = new StringBuffer();//保存尚未解码的数据结果
		String binaryStr= "01100001010001110101011001110011011000100100011100111000011001110101011000110010001110010111100101100010010001110101000101101000";//二进制数据,这里是取用上面程序的最后结果
		System.out.println("二进制数据:"+binaryStr);
		//这里采用正则表达式来匹配8位长度的数据,然后一个个find()
		Matcher matcher = Pattern.compile("\\d{8}").matcher(binaryStr);//定义匹配模式并,获取模式
		BASE64Decoder d = new BASE64Decoder();//解码器
		while(matcher.find()){//在binaryStr中找到了8位长度的数据,依次往后面找
			int intVal = Integer.valueOf(matcher.group(),2);//matcher.group()中存储了找到匹配模式的数据,这里以2进制的形式转换为整数
			results.append((char)intVal);//将整数转换为对应的字符,并添加到结果中
		}
		System.out.println("尚未解码的数据:"+results);//输出尚未解码的数据
		String s = new String(d.decodeBuffer(results.toString()));//得到解码后的数据
		System.out.println("解码后的数据:"+s);//输出解码后的数据
		System.out.println("=========二进制到字符串结束!=============");
	}
}



运行效果:

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

WJL_MGQS

本不富裕的收入,还得买服务器

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

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

打赏作者

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

抵扣说明:

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

余额充值