byte[]到short、int、long的相互转换
来源:互联网 发布:淘宝达人合作平台 编辑:程序博客网 时间:2024/05/18 22:40
public final static byte[] getBytes(short s, boolean asc) { byte[] buf = new byte[2]; if (asc) for (int i = buf.length - 1; i >= 0; i--) { buf[i] = (byte) (s & 0x00ff); s >>= 8; } else for (int i = 0; i < buf.length; i++) { buf[i] = (byte) (s & 0x00ff); s >>= 8; } return buf;} public final static byte[] getBytes(int s, boolean asc) { byte[] buf = new byte[4]; if (asc) for (int i = buf.length - 1; i >= 0; i--) { buf[i] = (byte) (s & 0x000000ff); s >>= 8; } else for (int i = 0; i < buf.length; i++) { buf[i] = (byte) (s & 0x000000ff); s >>= 8; } return buf;} public final static byte[] getBytes(long s, boolean asc) { byte[] buf = new byte[8]; if (asc) for (int i = buf.length - 1; i >= 0; i--) { buf[i] = (byte) (s & 0x00000000000000ff); s >>= 8; } else for (int i = 0; i < buf.length; i++) { buf[i] = (byte) (s & 0x00000000000000ff); s >>= 8; } return buf;} public final static short getShort(byte[] buf, boolean asc) { if (buf == null) { throw new IllegalArgumentException("byte array is null!"); } if (buf.length > 2) { throw new IllegalArgumentException("byte array size > 2 !"); } short r = 0; if (asc) for (int i = buf.length - 1; i >= 0; i--) { r <<= 8; r |= (buf[i] & 0x00ff); } else for (int i = 0; i < buf.length; i++) { r <<= 8; r |= (buf[i] & 0x00ff); } return r;} public final static int getInt(byte[] buf, boolean asc) { if (buf == null) { throw new IllegalArgumentException("byte array is null!"); } if (buf.length > 4) { throw new IllegalArgumentException("byte array size > 4 !"); } int r = 0; if (asc) for (int i = buf.length - 1; i >= 0; i--) { r <<= 8; r |= (buf[i] & 0x000000ff); } else for (int i = 0; i < buf.length; i++) { r <<= 8; r |= (buf[i] & 0x000000ff); } return r;} public final static long getLong(byte[] buf, boolean asc) { if (buf == null) { throw new IllegalArgumentException("byte array is null!"); } if (buf.length > 8) { throw new IllegalArgumentException("byte array size > 8 !"); } long r = 0; if (asc) for (int i = buf.length - 1; i >= 0; i--) { r <<= 8; r |= (buf[i] & 0x00000000000000ff); } else for (int i = 0; i < buf.length; i++) { r <<= 8; r |= (buf[i] & 0x00000000000000ff); } return r;}
0 0
- byte[]到short、int、long的相互转换
- byte[]到short、int、long的相互转换
- byte[]到short、int、long的相互转换
- byte[]到short、int、long的相互转换
- Java中有关byte和String的转换笔记和byte[]到short、int、long的相互转换
- short,int,long与byte数组之间的转换
- short、int、long与byte之间的转换工具类
- short、int、long与byte之间的转换工具类
- short、int、long与byte之间的转换工具类
- short、int、long与byte之间的转换工具类
- Android中int,short,long和byte的转换
- short、int、long与byte之间的转换工具类
- byte short int long
- byte、short、int、long
- Byte Short Int Long的长度
- java byte数组与int,long,short,byte转换
- java byte数组与int,long,short,byte转换
- byte short int long 学习
- vim中实现多行缩进
- MVP模式
- Android Studio Gradle的一些总结
- ZooKeeper学习总结(2)——ZooKeeper开源Java客户端ZkClient使用
- 数据库
- byte[]到short、int、long的相互转换
- 在树莓派/ArchlinuxArm上安装yaourt
- Material Design设计之Tinting(着色)和Clipping(裁剪)
- Git分支的前世今生
- 扫盲:字符编码与字符集的区别
- 用git下载代码很慢的问题解决
- node.js websocket+html5实现的简单联网tank大战的游戏模型
- 【Java+OpenCV】安装与配置
- linux修改默认的编辑器