Java byte数组按位转化为long类型
来源:互联网 发布:北京seo 编辑:程序博客网 时间:2024/06/06 19:33
本方法适用于(高位在前,低位在后)的顺序
public static void main(String[] args) { byte[] l = new byte[]{(byte) 0xe7, (byte) 0xf3, (byte) 0x90, (byte) 0x54, (byte) 0x39, (byte) 0x6e, (byte) 0x11, (byte) 0x7c, (byte) 0x01, (byte) 0xe7, (byte) 0x70, (byte) 0x3c, (byte) 0x79, (byte) 0x14, (byte) 0xb5, (byte) 0x67}; System.out.println(getLong(l,4));} /** * @param data 数组 * @param num 多少个byte相组合成一个数字 * */ public static long getLong(byte[] data, int num) { long count = 0; for (int i = 0; i < data.length / num; i++) { int offset = i * num; long val = 0; for (int j = 0; j < num; j++) { long _val = ((long)(data[offset + num - j - 1] & 0xFF) << j * 8); if (val == 0) { val = _val; } else { val = val | _val; } } count = count + val; } return count; }
输出:6918358899
本方法适用于(高位在前,低位在后)的顺序
public static void main(String[] args) { byte[] l = new byte[]{(byte) 0xe7, (byte) 0xf3, (byte) 0x90, (byte) 0x54, (byte) 0x39, (byte) 0x6e, (byte) 0x11, (byte) 0x7c, (byte) 0x01, (byte) 0xe7, (byte) 0x70, (byte) 0x3c, (byte) 0x79, (byte) 0x14, (byte) 0xb5, (byte) 0x67}; System.out.println(getLong(l,4)); }/** * @param data 数组 * @param num 多少个byte相组合成一个数字 * */ public static long getLong(byte[] data, int num) { long count = 0; for (int i = 0; i < data.length / num; i++) { int offset = i * num; long val = 0; for (int j = 0; j < num; j++) { long _val = ((long)(data[offset + j] & 0xFF) << j * 8); if (val == 0) { val = _val; } else { val = val | _val; } } count = count + val; } return count; }
输出:6918358899
阅读全文
0 0
- Java byte数组按位转化为long类型
- 关于java各种类型转化为无符号和类型转化为byte数组的方法
- JAVA的数组 && 原始类型和引用类型以及String与int long相互转化 && String与byte[]相互转化
- Java中byte数组与long数组相互转化
- Java中byte数组与long数组相互转化
- java中类型int转化为Byte
- java 把 byte[] 数组转化为String
- java中InputStream转化为byte[]数组
- Java 中String转化为byte数组
- java中InputStream转化为byte[]数组
- java中InputStream转化为byte[]数组
- java中InputStream转化为byte[]数组
- java中InputStream转化为byte[]数组
- int、long、float类型的数组转化成byte类型的数组
- java中Long类型转化为int类型
- Long类型 转化为String
- Byte数组转化为String
- inputStream转化为byte[]数组
- Robot Framework如何实现识别验证码
- 近期学习总结
- Android 绑定服务
- Java通过HttpProxy实现穿越
- Unity3D Protobuf-net实际操作教程一
- Java byte数组按位转化为long类型
- Java多线程中wait, notify and notifyAll的使用
- error MSB8031: Building an MFC project for a non-Unicode character set is deprecated
- opencv 链接共享库文件失败
- MySQL IFNULL()函数的用法
- 链表问题——删除链表的中间节点和a/b处的节点
- iptables nat及端口映射 【转】
- Camera原理
- 【队内胡策 11.3】 T1 T2