Java中整形和byte数组相互转换,支持正负数
2016-04-09 16:33
1251 查看
/** * 字节数组转int,适合转高位在前低位在后的byte[] * * @param bytes * @return */ public static long byteArrayToLong(byte[] bytes) { ByteArrayInputStream bais = new ByteArrayInputStream(bytes); DataInputStream dis = new DataInputStream(bais); long result = 0; try { int len = dis.available(); if (len == 1) { result = dis.readByte(); } else if (len == 2) { result = dis.readShort(); } else if (len == 4) { result = dis.readInt(); } else if (len == 8) { result = dis.readLong(); } } catch (IOException e) { SyncLogUtil.e(e); } finally { try { dis.close(); bais.close(); } catch (IOException e) { SyncLogUtil.e(e); } } return result; } /** * int转byte[],高位在前低位在后 * * @param value * @return */ public static byte[] varIntToByteArray(long value) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream oos = new DataOutputStream(baos); Long l = new Long(value); try { if (l == l.byteValue()) { oos.writeByte(l.byteValue()); } else if (l == l.shortValue()) { oos.writeShort(l.shortValue()); } else if (l == l.intValue()) { oos.writeInt(l.intValue()); } else if (l == l.longValue()) { oos.writeLong(l.longValue()); } else if (l == l.floatValue()) { oos.writeFloat(l.floatValue()); } else if (l == l.doubleValue()) { oos.writeDouble(l.doubleValue()); } } catch(IOException e) { SyncLogUtil.e(e); } finally { try { baos.close(); oos.close(); } catch (IOException e) { SyncLogUtil.e(e); } } return baos.toByteArray(); }
进入DataOutputStream查看它的write方法实现改造上述方法后:
/** * 字节数组转int,适合转高位在前低位在后的byte[] * * @param bytes * @return */ public static long byteArrayToLong(byte[] bytes) { long result = 0; int len = bytes.length; if (len == 1) { byte ch = (byte) (bytes[0] & 0xff); result = ch; } else if (len == 2) { int ch1 = bytes[0] & 0xff; int ch2 = bytes[1] & 0xff; result = (short) ((ch1 << 8) | (ch2 << 0)); } else if (len == 4) { int ch1 = bytes[0] & 0xff; int ch2 = bytes[1] & 0xff; int ch3 = bytes[2] & 0xff; int ch4 = bytes[3] & 0xff; result = (int) ((ch1 << 24) | (ch2 << 16) | (ch3 << 8) | (ch4 << 0)); } else if (len == 8) { long ch1 = bytes[0] & 0xff; long ch2 = bytes[1] & 0xff; long ch3 = bytes[2] & 0xff; long ch4 = bytes[3] & 0xff; long ch5 = bytes[4] & 0xff; long ch6 = bytes[5] & 0xff; long ch7 = bytes[6] & 0xff; long ch8 = bytes[7] & 0xff; result = (ch1 << 56) | (ch2 << 48) | (ch3 << 40) | (ch4 << 32) | (ch5 << 24) | (ch6 << 16) | (ch7 << 8) | (ch8 << 0); } return result; } /** * int转byte[],高位在前低位在后 * * @param value * @return */ public static byte[] varIntToByteArray(long value) { Long l = new Long(value); byte[] valueBytes = null; if (l == l.byteValue()) { valueBytes = toBytes(value, 1); } else if (l == l.shortValue()) { valueBytes = toBytes(value, 2); } else if (l == l.intValue()) { valueBytes = toBytes(value, 4); } else if (l == l.longValue()) { valueBytes = toBytes(value, 8); } return valueBytes; } private static byte[] toBytes(long value, int len) { byte[] valueBytes = new byte[len]; for (int i = 0;i < len;i++) { valueBytes[i] = (byte) (value >>> 8 * (len - i - 1)); } return valueBytes; }
测试代码:
private static void check(long s, long r, String tag) { if (s == r) { System.out.println(tag + "[result:" + r + "]"); } else { System.err.println(tag + "[source:" + s + ",result:" + r + "]"); } } public static void main(String[] args) throws Exception { // byte byte bMax = Byte.MAX_VALUE; byte bMin = Byte.MIN_VALUE; long b_max_v = byteArrayToLong(varIntToByteArray(bMax)); check(bMax, b_max_v, "byte"); long b_min_v = byteArrayToLong(varIntToByteArray(bMin)); check(bMin, b_min_v, "byte"); // short short sMax = Short.MAX_VALUE; short sMin = Short.MIN_VALUE; long s_max_v = byteArrayToLong(varIntToByteArray(sMax)); check(sMax, s_max_v, "short"); long s_min_v = byteArrayToLong(varIntToByteArray(sMin)); check(sMin, s_min_v, "short"); //int int iMax = Integer.MAX_VALUE; int iMin = Integer.MIN_VALUE; long i_max_v = byteArrayToLong(varIntToByteArray(iMax)); check(iMax, i_max_v, "int"); long i_min_v = byteArrayToLong(varIntToByteArray(iMin)); check(iMin, i_min_v, "int"); // long long lMax = Long.MAX_VALUE; long lMin = Long.MIN_VALUE; long l_max_v = byteArrayToLong(varIntToByteArray(lMax)); check(lMax, l_max_v, "long"); long l_min_v = byteArrayToLong(varIntToByteArray(lMin)); check(lMin, l_min_v, "long"); }
相关文章推荐
- java 数组的运用
- java集合框架的总结
- java版的生产者消费者模型
- JAVA泛型中的类型擦除及为什么不支持泛型数组
- java学习系列----反射
- 【SVN】修改MyEclipse中的SVN地址
- 20145236 《Java程序设计》 第6周学习总结
- eclipse最有用快捷键整理
- 针对中科院java接口的使用方法和问题
- 比较字符串是否相等
- Java基础
- java--连接数据库--mysql--介绍
- java 递归算法
- Struts2源码分析(二)Struts2运行流程分析
- Java中super的几种用法并与this的区别
- struts整合之 spring 对于 destroy listen等问题
- java.net.BindException: Address already in use: JVM_Bind <null>:8001解决办法
- JAVA round 和 floor的区别
- 基于Spring3 MVC实现批量导出数据成Excel文件!
- 2.1 使用eclipse4.4 搭建 maven简单结构项目。