HexUtil工具类
来源:互联网 发布:昆仑墟麒麟臂升阶数据 编辑:程序博客网 时间:2024/06/05 03:33
- /**
- * 进制转化
- * @author
- *
- */
- public class HexUtil {
- /**
- * 二进制byte数组转十六进制byte数组
- * byte array to hex
- *
- * @param b byte array
- * @return hex string
- */
- public static String byte2hex(byte[] b) {
- StringBuilder hs = new StringBuilder();
- String stmp;
- for (int i = 0; i < b.length; i++) {
- stmp = Integer.toHexString(b[i] & 0xFF).toUpperCase();
- if (stmp.length() == 1) {
- hs.append("0").append(stmp);
- } else {
- hs.append(stmp);
- }
- }
- return hs.toString();
- }
- /**
- * 十六进制byte数组转二进制byte数组
- * hex to byte array
- *
- * @param hex hex string
- * @return byte array
- */
- public static byte[] hex2byte(String hex)
- throws IllegalArgumentException{
- if (hex.length() % 2 != 0) {
- throw new IllegalArgumentException ("invalid hex string");
- }
- char[] arr = hex.toCharArray();
- byte[] b = new byte[hex.length() / 2];
- for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {
- String swap = "" + arr[i++] + arr[i];
- int byteint = Integer.parseInt(swap, 16) & 0xFF;
- b[j] = new Integer(byteint).byteValue();
- }
- return b;
- }
- public static void main(String[] args) {
- String str1 = "abcedefghijklmnopqrstuvwxyz";
- //String str1 = "1";
- String hexStr = HexUtil.byte2hex(str1.getBytes());
- System.out.println(hexStr);
- String str2 = new String(HexUtil.hex2byte(hexStr));
- System.out.println(str2);
- System.out.println(str1.equals(str2));
- }
- }
阅读全文
0 0
- HexUtil工具类
- HexUtil工具类
- 工具类
- 工具类!!!!
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- VBA处理文件框架代码 【第三部分:具体处理】
- 求出二进制中1的个数(葵花宝典你值得拥有)
- HashMap的数据结构
- 金融数据的尖峰厚尾
- 安卓工作室 android studio 汉化后,报错。 设置界面打不开。Can't find resource for bundle java.util.PropertyResourceBundle
- HexUtil工具类
- idea 开发插件。
- hdu 6096 String(AC自动机)
- 你也可以找到好工作(三)大结局
- android studio java工程 报错
- centos7搭建nexus maven私服
- HDU-6105 Gameia
- 关于HttpServletRequest对象
- android monitor 汉化 ddms