【Java】short、int、float、boolean与byte之间的转换工具类
来源:互联网 发布:java soap 编辑:程序博客网 时间:2024/06/05 19:59
import java.nio.ByteBuffer;/** * 各基础类型与byte之间的转换 **/public class Utility { /** * 将boolean转成byte[] * @param val * @return byte[] */ public static byte[] Boolean2ByteArray(boolean val) { int tmp = (val == false) ? 0 : 1; return ByteBuffer.allocate(4).putInt(tmp).array(); } /** * 将byte[]转成boolean * @param data * @return boolean */ public static boolean ByteArray2Boolean(byte[] data) { if (data == null || data.length < 4) { return false; } int tmp = ByteBuffer.wrap(data, 0, 4).getInt(); return (tmp == 0) ? false : true; } /** * 将int转成byte[] * @param val * @return byte[] */ public static byte[] Int2ByteArray(int val) { return ByteBuffer.allocate(4).putInt(val).array(); } /** * 将byte[]转成int * @param data * @return int */ public static int ByteArray2Int(byte[] data) { if (data == null || data.length < 4) { return 0xDEADBEEF; } return ByteBuffer.wrap(data, 0, 4).getInt(); } /** * 将float转成byte[] * @param val * @return byte[] */ public static byte[] Float2ByteArray(float val) { return ByteBuffer.allocate(4).putFloat(val).array(); } /** * 将byte[]转成float * @param data * @return float */ public static float ByteArray2Float(byte[] data) { if (data == null || data.length < 4) { return -1234.0f; } return ByteBuffer.wrap(data).getFloat(); } /** * 将byte[]数组转成short[]数组 * @param b * @return short[] */ public static short[] byteArray2ShortArray(byte[] b) { int len = b.length / 2; int index = 0; short[] re = new short[len]; byte[] buf = new byte[2]; for (int i = 0; i < b.length;) { buf[0] = b[i]; buf[1] = b[i + 1]; short st = byteToShort(buf); re[index] = st; index++; i += 2; } return re; } /** * 将一个short[]数组反转为byte[]字节数组 * @param b */ public static byte[] shortArray2ByteArray(short[] b) { byte[] rebt = new byte[b.length * 2]; int index = 0; for (int i = 0; i < b.length; i++) { short st = b[i]; byte[] bt = shortToByte(st); rebt[index] = bt[0]; rebt[index + 1] = bt[1]; index += 2; } return rebt; } /** * short转换为byte[] * @param number * @return byte[] */ public static byte[] shortToByte(short number) { int temp = number; byte[] b = new byte[2]; // 将最低位保存在最低位 b[0] = (byte)(temp & 0xff); temp = temp >> 8; // 向右移8位 b[1] = (byte)(temp & 0xff); return b; } /** * byte[]转换为short * @param b * @return short */ public static short byteToShort(byte[] b) { short s = 0; short s0 = (short) (b[0] & 0xff);// 最低位 short s1 = (short) (b[1] & 0xff); s1 <<= 8; s = (short) (s0 | s1); return s; }}
0 0
- 【Java】short、int、float、boolean与byte之间的转换工具类
- short、int、long与byte之间的转换工具类
- short、int、long与byte之间的转换工具类
- short、int、long与byte之间的转换工具类
- short、int、long与byte之间的转换工具类
- short、int、long与byte之间的转换工具类
- java中byte数组与int,long,short,float,char之间的转换
- java中byte数组与int,long,short,float,char之间的转换
- Java中byte数组与short,int,char,long,float,double之间的转换
- Java中byte数组与short,int,char,long,float,double之间的转换
- short,int,long,double,string,float与byte数组之间的转换
- java Byte和各数据类型(short,int,long,float,double)之间的转换
- java Byte和各数据类型(short,int,long,float,double)之间的转换
- byte 和boolean,int,String,char,short,long之间的转换
- short,int,long与byte数组之间的转换
- Java异常处理及异常机制介绍 && long,int,short与byte数组之间的转换
- Java 语言中 short 与 byte[]之间的转换,int、long 类似。
- java中long,int,short与byte数组之间的转换
- Android 发送get请求 URL中文参数显示问号的一种解决办法
- h5的小应用加tp3.2 ajax
- java中特殊的类
- Activity 任务栈分析与及Activity的启动方式
- 大数问题
- 【Java】short、int、float、boolean与byte之间的转换工具类
- Mybatis -代码自动生成(generatorConfig.xml)
- OJ编程题输入数据处理
- could not resolve property: username of: meituan.bean.User [from meituan.bean.User as u where u.user
- 中国剩余定理
- C++对象在内存中的存放方式
- C#游戏《飞机大战》开发中的错误
- 正则表达式详谈及实例
- iBET Casino LuckyDraw ADIDAS YEEZY- SKY3888 Casino