admin 管理员组文章数量: 887021
2024年2月19日发(作者:html手机模拟器)
if (null == bytes || beginIndex < 0 || < beginIndex + 8) {return -1;}try {bytes[beginIndex++] = (byte) (data & 0xff);bytes[beginIndex++] = (byte) (data >> 8 & 0xff);bytes[beginIndex++] = (byte) (data >> 16 & 0xff);bytes[beginIndex++] = (byte) (data >> 24 & 0xff);bytes[beginIndex++] = (byte) (data >> 32 & 0xff);bytes[beginIndex++] = (byte) (data >> 40 & 0xff);bytes[beginIndex++] = (byte) (data >> 48 & 0xff);bytes[beginIndex++] = (byte) (data >> 56 & 0xff);} catch (RuntimeException e) {throw e;}return beginIndex;}/*** 字节数组转整型** @param bytes 待转换数组* @param beginIndex 数组起始下标* @return*/public static int bytes2Int(byte[] bytes, int beginIndex) {if (null == bytes || beginIndex < 0 || < beginIndex + 4) {return -1;}int result = 0;try {int tmpVal = 0;for (int i = 0; i < 4; i++) {tmpVal = (bytes[i + beginIndex] << (i * 8));switch (i) {
case 0:tmpVal = tmpVal & 0x000000FF;break;case 1:tmpVal = tmpVal & 0x0000FF00;break;case 2:tmpVal = tmpVal & 0x00FF0000;break;case 3:tmpVal = tmpVal & 0xFF000000;break;}result |= tmpVal;}} catch (RuntimeException e) {throw e;}return result;}/*** 字节数组转长整型** @param bytes 待转换数组* @param beginIndex 数组起始下标* @return*/public static long bytes2Long(byte[] bytes, int beginIndex) {if (null == bytes || beginIndex < 0 || < beginIndex + 8) {return -1L;}long result = 0L;try {int shift = 0;
for (int i = 0; i < 8; i++) {shift = i << 3;result |= ((long) 0xff << shift) & ((long) bytes[i + beginIndex] << shift);}} catch (RuntimeException e) {throw e;}return result;}
版权声明:本文标题:java数字转数组_Java中数值类型与字节数组之间的转换大法(精简)_ 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1708330630h520038.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论