字节工具
来源:互联网 发布:nginx ssl配置详解 编辑:程序博客网 时间:2024/05/17 04:02
public class BytesUtil { public static char[] hex = "0123456789ABCDEF".toCharArray(); public static String bytesToHexStr(byte... bytes) { char[] result = new char[bytes.length * 2]; int resultIndex; for (int i = 0; i < bytes.length; i++) { resultIndex = 2 * i; result[resultIndex] = hex[(bytes[i] >> 4) & 0x0F]; result[resultIndex + 1] = hex[bytes[i] & 0x0F]; } return new String(result); } public static byte[] hexStrToBytes(String byteStr) { if (byteStr == null) { return null; } if (byteStr.length() % 2 != 0) { throw new RuntimeException("byteStr长度不是偶数,没法转"); } int resultLen = byteStr.length() / 2; byte[] result = new byte[resultLen]; char h, l; byte b; byteStr = byteStr.toUpperCase(); int strIndex; for (int i = 0; i < resultLen; i++) { strIndex = 2 * i; h = byteStr.charAt(strIndex); if ('0' <= h && h <= '9') { b = (byte) ((h - '0') << 4); } else if ('A' <= h && h <= 'F') { b = (byte) ((h - 'A' + 10) << 4); } else { throw new RuntimeException("byteStr 的第" + i + "个字符 不在16进制范围中"); } l = byteStr.charAt(strIndex + 1); if ('0' <= l && l <= '9') {// b |= (byte) (0x0F & (l - '0')); b |= (byte) (l - '0'); } else if ('A' <= l && l <= 'F') {// b |= (byte) (0x0F & (l - 'A' + 10)); b |= (byte) (l - 'A' + 10); } else { throw new RuntimeException("byteStr 的第" + (i + 1) + "个字符 不在16进制范围中"); } result[i] = b; } return result; }}
0 0
- 字节工具
- 字节工具
- 还原字节集工具
- 字节补丁生成工具
- 字节码转换工具
- Java字节码反编译工具
- 【java工具类】字节操作
- Java 字节码 工具 javassist
- class文件字节码分析工具:javap
- asm java字节码操控工具学习
- 工具类--本地路径图片转字节
- ASM(字节码处理工具)
- 对象字节数组转换工具类
- 字节流转为字符串的工具类
- 字节?
- 字节
- 字节
- 字节
- iOS使用AFNetWorking获取网络数据时遇到的问题??
- ndk-stack的使用
- iOS 万能跳转界面方法 (runtime实用篇一)
- 1---JVM调优总结 -Xms -Xmx -Xmn -Xss
- iOS 代理为啥要用weak修饰? (刨根问底一)
- 字节工具
- pthread_create线程创建的过程剖析 http://blog.csdn.net/yetyongjin/article/details/7673837
- 关于银联支付和提现切换服务器出现的种种问题
- 对自己的要求
- Maven实践(二):Spring与classpath*加载配置文件
- StringTokenizer类的使用
- hdu1166 树状数组
- HDU 2527 Safe Or Unsafe
- 对一个UiView添加点击事件