最近的项目频繁涉及大小端转换的情况,参考github(https://github.com/Frank-Wiebeler/java-Big2LittleEndian/blob/master/src/com/dev4ag/Big2LittleEndian.java)自己改了一个大小端转换的工具类,记录一下;

package com.ruijie.iotp.common.webcomponent.util;/*** Description 大小端转换工具类* */
public class EndianUtil {public static byte[] short2LittleEndianBytes(short val){return short2Bytes(short2LittleEndian(val));}public static byte[] int2LittleEndianBytes(int val){return int2Bytes(int2LittleEndian(val));}public static byte[] long2LittleEndianBytes(long val){return long2Bytes(long2LittleEndian(val));}/*** Reformats a Little Endian value to bigEndian* @param val the value to transform* @return Big Endian Value*/public static short short2LittleEndian(short val){return (short)(((val&0xff00)>>>8) + ((val&0x00ff)<<8));}/*** Reformats a Little Endian value to bigEndian* @param val the value to transform* @return Big Endian Value*/public static int int2LittleEndian(int val){return(  ( val&0xff000000 ) >>> 24 ) +( (( val&0x00ff0000 ) >>> 16 ) << 8 ) +( (( val&0x0000ff00 ) >>>  8 ) << 16 ) +( (( val&0x000000ff )        ) << 24 );}/*** Reformats a Little Endian value to bigEndian* @param val the value to transform* @return Big Endian Value*/public static long long2LittleEndian(long val){return( (val&0xff00000000000000L) >>> 56 ) +(((val&0x00ff000000000000L) >>> 48 ) << 8 ) +(((val&0x0000ff0000000000L) >>> 40 ) << 16 ) +(((val&0x000000ff00000000L) >>> 32 ) << 24 ) +(((val&0x00000000ff000000L) >>> 24 ) << 32 ) +(((val&0x0000000000ff0000L) >>> 16 ) << 40 ) +(((val&0x000000000000ff00L) >>> 8  ) << 48 ) +(((val&0x00000000000000ffL)        ) << 56 );}/*** ================================================================================*          The other way arround*          Fun fact: If you turn the bytes, the bytes turn. If you do it twice, you undo it.*          Turn AB to BA and turn it again => AB. Magic :)*          So, it's all basically the same functions.**          However, we create some alias functions to make it*          readable in your source code, what you assume to do :)*//*** Reformats a Little Endian value to bigEndian* @param val the value to transform* @return Big Endian Value*/public static int intFromLittleEndian(int val){return int2LittleEndian(val);}public static short shortFromLittleEndian(short val){return short2LittleEndian(val);}public static long longFromLittleEndian(long val){return long2LittleEndian(val);}public static byte[] long2Bytes(long a) {return new byte[] {(byte) ((a >> 56) & 0xFF),(byte) ((a >> 48) & 0xFF),(byte) ((a >> 40) & 0xFF),(byte) ((a >> 32) & 0xFF),(byte) ((a >> 24) & 0xFF),(byte) ((a >> 16) & 0xFF),(byte) ((a >> 8) & 0xFF),(byte) (a & 0xFF)};}public static byte[] int2Bytes(int a) {return new byte[] {(byte) ((a >> 24) & 0xFF),(byte) ((a >> 16) & 0xFF),(byte) ((a >> 8) & 0xFF),(byte) (a & 0xFF)};}public static byte[] short2Bytes(short a) {return new byte[] {(byte) ((a >> 8) & 0xFF),(byte) (a & 0xFF)};}public static long bytes2Long(byte[]bytes) {return (0xffL & (long)bytes[7])| (0xff00L & ((long)bytes[6] << 8))| (0xff0000L & ((long)bytes[5] << 16))| (0xff000000L & ((long)bytes[4] << 24))| (0xff00000000L & ((long)bytes[3] << 32))| (0xff0000000000L & ((long)bytes[2] << 40))| (0xff000000000000L & ((long)bytes[1] << 48))| (0xff00000000000000L & ((long)bytes[0] << 56));}public static long bytes2LongBigEndian(byte[]bytes) {return (0xffL & (long)bytes[0])| (0xff00L & ((long)bytes[1] << 8))| (0xff0000L & ((long)bytes[2] << 16))| (0xff000000L & ((long)bytes[3] << 24))| (0xff00000000L & ((long)bytes[4] << 32))| (0xff0000000000L & ((long)bytes[5] << 40))| (0xff000000000000L & ((long)bytes[6] << 48))| (0xff00000000000000L & ((long)bytes[7] << 56));}public static int bytes2Int(byte[]bytes) {return (bytes[0]&0xff) << 24| (bytes[1]&0xff) << 16| (bytes[2]&0xff) << 8| (bytes[3]&0xff);}public static int bytes2IntBigEndian(byte[]bytes) {return (bytes[3]&0xff) << 24| (bytes[2]&0xff) << 16| (bytes[1]&0xff) << 8| (bytes[0]&0xff);}public static short bytes2Short(byte[]bytes) {return  (short)((bytes[0]&0xff) << 8| (bytes[1]&0xff));}public static int bytes2ShortBigEndian(byte[]bytes) {return  (short)((bytes[1]&0xff) << 8| (bytes[0]&0xff));}
}

另外ByteBuffer好像也有对应的处理方法,写了一些简单的测试方法代码如下:

    private static ByteBuffer shortBuffer = ByteBuffer.allocate(2);private static ByteBuffer intBuffer = ByteBuffer.allocate(4);private static ByteBuffer longBuffer = ByteBuffer.allocate(8);public static long bytes2Short(byte[] bytes) {shortBuffer.clear();shortBuffer.put(bytes, 0, bytes.length);shortBuffer.flip();//need flipreturn shortBuffer.getShort();}public static long bytes2ShortBigEndian(byte[] bytes) {shortBuffer.clear();shortBuffer.order(ByteOrder.LITTLE_ENDIAN);shortBuffer.put(bytes, 0, bytes.length);shortBuffer.flip();//need flipreturn shortBuffer.getShort();}public static long bytes2ShortBigEndian(byte[] bytes, int pos) {shortBuffer.clear();shortBuffer.order(ByteOrder.LITTLE_ENDIAN);shortBuffer.put(bytes, pos, 2);shortBuffer.flip();//need flipreturn shortBuffer.getShort();}public static long bytes2Int(byte[] bytes) {intBuffer.clear();intBuffer.put(bytes, 0, bytes.length);intBuffer.flip();//need flipreturn intBuffer.getInt();}public static long bytes2IntBigEndian(byte[] bytes) {intBuffer.clear();intBuffer.order(ByteOrder.LITTLE_ENDIAN);intBuffer.put(bytes, 0, bytes.length);intBuffer.flip();//need flipreturn intBuffer.getInt();}public static long bytes2IntBigEndian(byte[] bytes, int pos) {intBuffer.clear();intBuffer.order(ByteOrder.LITTLE_ENDIAN);intBuffer.put(bytes, pos, 4);intBuffer.flip();//need flipreturn intBuffer.getInt();}public static long bytes2Long(byte[] bytes) {longBuffer.clear();longBuffer.put(bytes, 0, bytes.length);longBuffer.flip();//need flipreturn longBuffer.getLong();}public static long bytes2LongBigEndian(byte[] bytes) {longBuffer.clear();longBuffer.order(ByteOrder.LITTLE_ENDIAN);longBuffer.put(bytes, 0, bytes.length);longBuffer.flip();//need flipreturn longBuffer.getLong();}public static long bytes2LongBigEndian(byte[] bytes, int pos) {longBuffer.clear();longBuffer.order(ByteOrder.LITTLE_ENDIAN);longBuffer.put(bytes, pos, 8);longBuffer.flip();//need flipreturn longBuffer.getLong();}

java大小端转换工具类相关推荐

  1. Android大小单位转换工具类

    package com.utouu.im.util; /**  * Android大小单位转换工具类  *   *   */ public class DisplayUtil { /** * 将px值 ...

  2. 分享万能java字符串编码转换工具类

    代码下载地址:http://www.zuidaima.com/share/1795356301560832.htm 原文:分享万能java字符串编码转换工具类 package com.zuidaima ...

  3. Java 字体颜色转换工具类 ColorUtil

    import java.awt.Color;  import jxl.format.Colour;       /**  *字体颜色转换工具类  * @author tanghui  *  */ pu ...

  4. Java 大小端转换(基于ByteBuffer)

    大小端的基础知识: 小端 ( little-endian):低位字节在前,高位字节在后.大端(Big-Endian),则反之.具体而言,就是为了说清楚,CPU架构中1字(word)的存储顺序.计算机内 ...

  5. Java 进制转换工具类

    /*** 进制转换工具类* @author dell**/ public class HexadecimalUtil {/*** 获得倒序二进制数据* @param hexString* @retur ...

  6. java 数字大小写转换工具类--适用于打印收据

    本工具类主要实现数字double1000.00====>>零万壹仟零佰零拾零元零角零分,注释很详细,就不多做介绍了 /*** 不考虑分隔符的正确性*/private static fina ...

  7. JAVA 进制转换工具类 10进制转16进制 10进制转2进制 16进制转2进制 16进制10进制 2进制转10进制 2进制转16进制

    进制转换的坑: 注意:使用Integer的方法转换出来的2进制字符串 最高位是没有补零的 ,java的Integer类的toBinaryString()方法转换出来的二进制字符串只会保留最高非零位以后 ...

  8. java万能编码转换_分享万能java字符串编码转换工具类

    package com.zuidaima.util; import java.io.UnsupportedEncodingException; /** * 转换字符串的编码 */ public cla ...

  9. android 字体像素转换工具类_android px,dp,sp大小转换工具

    package com.voole.playerlib.util; import android.content.Context; /** * Android大小单位转换工具类 * * float s ...

最新文章

  1. 2.1 为什么要进行实例探究-深度学习第四课《卷积神经网络》-Stanford吴恩达教授
  2. C#委托、事件、消息(入门级)
  3. hdu 5036 Explosion bitset优化floyd
  4. 使用IntelliJ IDEA 2020 高效开发 springboot项目
  5. Mysql的Root密码忘记,查看或修改的解决方法(图文介绍)
  6. pdo mysql 绑定查询_php mysql PDO 查询操作的实例详解
  7. linux安装chrome_chrome 的安装及出现密钥问题(ubuntu16.04)
  8. 苹果cms小主题模板
  9. 多节点部署执行定时任务选举单一节点解决方案---redis
  10. 开心庄园html的代码,HTML第五章(示例代码)
  11. echarts饼状图
  12. Selenium04-selenium中的Xpath元素定位方法爬虫实践
  13. CAD数字签名的实现
  14. Android攻城狮AsyncTask
  15. 代码 比较工具 在线
  16. RS Meet DL(68)-建模多任务学习中任务相关性的模型MMoE
  17. zufe 2527问题 K: Jelly与狗头人的地下世界
  18. ANSI编码文件批量转换为UTF-8编码小tips
  19. c语言程序设计李学刚戴白刃答案,李学刚
  20. ubuntu ip地址修改

热门文章

  1. AlertDialog的种类和使用方法
  2. java截取0_JAVA 字符串截取,长度不够以 0 补充
  3. VMware ESXi 8.0b Unlocker OEM BIOS 集成 REALTEK 网卡驱动和 NVMe 驱动 (集成驱动版)
  4. Serializable是什么,为什么要实现Serializable接口?
  5. 二、配置VS2019中Azure Kinect SDK环境——Azure Kinect DK入门
  6. 知识图谱构建的一般流程
  7. js定时器setTimeout和setInterval用法及区别,清除定时器的使用
  8. Mac Os X 系统,Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list-2.xml
  9. Android 7.1 双卡双待机器,首选网络类型设置 详细分析
  10. 基于java语言的输出全部的大写英文字母