byte 常用转换
来源:互联网 发布:剑三喵太捏脸数据 编辑:程序博客网 时间:2024/04/30 16:34
<span style="font-size:18px;">一些常用的bytes转换 。 写着备用 /** * 将int转换为四个字节的byte数组,高位在前,低位在后 * * @param i * 要转换的int参数 * @return 四个字节的byte数组 */ public static byte[] intToHlBytes4(int i) { byte[] result = new byte[4]; result[3] = (byte) (i & 0xFF); System.out.println(result[3]); result[2] = (byte) ((i >> 8) & 0xFF); System.out.println(result[2]); result[1] = (byte) ((i >> 16) & 0xFF); result[0] = (byte) ((i >> 24) & 0xFF); return result; } /** * 将int转换为四个字节的byte数组,低位在前,高位在后 * * @param i * 要转换的int参数 * @return 四个字节的byte数组 */ public static byte[] intTolhBytes4(int i) { byte[] result = new byte[4]; result[0] = (byte) (i & 0xFF); result[1] = (byte) ((i >> 8) & 0xFF); result[2] = (byte) ((i >> 16) & 0xFF); result[3] = (byte) ((i >> 24) & 0xFF); return result; } /** * 从byte数组的指定位置向后取出4位转为int数值,低位在前,高位在后 * * @param bs * 原始数组 * @param startSet * 开始位 * @return */ public static int hlBytesToInt(byte[] bs, int startSet) { int result; result = (int) ((bs[startSet] & 0xFF) | ((bs[startSet + 1] & 0xFF) << 8) | ((bs[startSet + 2] & 0xFF) << 16) | ((bs[startSet + 3] & 0xFF) << 24)); return result; } /** * 从byte数组的指定位置向后取出4位转为int数值,高位在前,低位在后 * * @param bs * 原始数组 * @param startSet * 开始位 * @return */ public static int bytesToInt2(byte[] bs, int startSet) { int result; result = (int) (((bs[startSet] & 0xFF) << 24) | ((bs[startSet + 1] & 0xFF) << 16) | ((bs[startSet + 2] & 0xFF) << 8) | (bs[startSet + 3] & 0xFF)); return result; } /** * long型转换为8字节的byte数组 高位在前低位在后 * * @param l * long数据 * @return */ public static byte[] longToHlBytes8(long l) { byte[] result = new byte[8]; for (int i = 0; i < 8; i++) { int startSet = (result.length - 1 - i) * 8; result[i] = (byte) ((l >>> startSet) & 0xFF); } return result; } /** * short整数转换为2字节的byte数组 高位在前低位在后 * * @param s * short整数 * @return */ public static byte[] unsignedShortToByte2(int s) { byte[] result = new byte[2]; result[0] = (byte) (s >> 8 & 0xFF); result[1] = (byte) (s & 0xFF); return result; } /** * byte数组转换为无符号short整数 * * @param bs * byte数组 * @return */ public static int byte2ToUnsignedShort(byte[] bs) { return byte2ToUnsignedShort(bs, 0); } /** * byte数组转换为无符号short整数 * * @param bs * byte数组 * @param startSer * 开始位置 * @return */ public static int byte2ToUnsignedShort(byte[] bs, int startSer) { int high = bs[startSer]; int low = bs[startSer + 1]; return (high << 8 & 0xFF00) | (low & 0xFF); } /** * byte数组转换为int整数 * * @param bs * byte数组 * @param startSet * 开始位置 * @return int整数 */ public static int byte4ToInt(byte[] bs, int startSet) { int b0 = bs[startSet] & 0xFF; int b1 = bs[startSet + 1] & 0xFF; int b2 = bs[startSet + 2] & 0xFF; int b3 = bs[startSet + 3] & 0xFF; return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3; }</span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;"></span>
转载请注明出处:http://blog.csdn.net/chenlinfeng772885775/article/details/50833849
0 0
- byte 常用转换
- Java常用数据类型转换成byte工具类
- 与byte数组有关的常用的类型转换总结
- int/byte/byte[]转换 c#
- Byte[]和byte[]的转换
- TCHAR*和BYTE*转换
- 字节转换(Byte Conversion)
- file和byte转换
- stream 与 byte[] 转换
- c# int byte转换
- byte转换成int
- string byte[]的转换
- C# struct byte[] 转换
- C# string byte[] 转换
- Java byte[] long 转换
- string int byte转换
- byte[]转换成InputStream
- c# byte[] string 转换
- Swift中的typealias
- C++ 文件查找 _findfirst、_findnext和_fineclose的使用
- Locating Elements
- 只刷新某一行的数据
- httpclient调用cxf开发webservice接口以及jboss开发的webservice接口
- byte 常用转换
- Ubunt15.04 安装JDK 8
- 生产环境下JAVA进程高CPU占用故障排查
- 69 - Sqrt(x)
- Windows XP Embedded 上手指南
- IE9以下不支持video问题
- 不能买到的数目
- Oracle用户和模式的区别
- Chrome DevTools