BitConverter.java:位处理字节数组处理工具类

简介: 字节处理工具类,可以从byte[]数组中读取Int16,Int32,Int64,以及int或long与byte[]数组的相互转换。


字节处理工具类,可以从byte[]数组中读取Int16,Int32,Int64,以及int或long与byte[]数组的相互转换。

package com.itshidu.util;
/**

  • 位处理,字节处理
  • @author Master.Xia Date:2017年12月15日
    */
    public class BitConverter {

    public static short ToInt16(byte[] bytes, int offset) {

     short result = (short) ((int)bytes[offset]&0xff);
     result |= ((int)bytes[offset+1]&0xff) << 8;
     return (short) (result & 0xffff);
    

    }
    public static int ToUInt16(byte[] bytes, int offset) {

     int result = (int)bytes[offset+1]&0xff;
     result |= ((int)bytes[offset]&0xff) << 8;
     return result & 0xffff;
    

    }
    public static int ToInt32(byte[] bytes, int offset) {

     int result = (int)bytes[offset]&0xff;
     result |= ((int)bytes[offset+1]&0xff) << 8;
     result |= ((int)bytes[offset+2]&0xff) << 16;
     result |= ((int)bytes[offset+3]&0xff) << 24;
     return result;
    

    }
    public static long ToUInt32(byte[] bytes, int offset) {

     long result = (int)bytes[offset]&0xff;
     result |= ((int)bytes[offset+1]&0xff) << 8;
     result |= ((int)bytes[offset+2]&0xff) << 16;
     result |= ((int)bytes[offset+3]&0xff) << 24;
     return result & 0xFFFFFFFFL;
    

    }
    public static long ToInt64(byte[] buffer,int offset) {

     long values = 0;
     for (int i = 0; i < 8; i++) {
         values <<= 8; values |= (buffer[offset+i] & 0xFF);
     }
     return values;
    

    }
    public static long ToUInt64(byte[] bytes, int offset) {

     long result = 0;
     for (int i = 0; i <= 56; i += 8) {
         result |= ((int)bytes[offset++]&0xff) << i;
     }
     return result;
    

    }
    public static float ToFloat(byte[] bs, int index) {

     return Float.intBitsToFloat(ToInt32(bs, index));                  
    

    }
    public static double ToDouble(byte[] arr,int offset) {

     return Double.longBitsToDouble(ToUInt64(arr, offset));
    

    }
    public static boolean ToBoolean(byte[] bytes,int offset) {

     return (bytes[offset]==0x00)? false:true;
    

    }

    public static byte[] GetBytes(short value) {

     byte[] bytes = new byte[2];
     bytes[0] = (byte) (value & 0xff);
     bytes[1] = (byte) ((value & 0xff00) >> 8);
     return bytes;
    

    }
    public static byte[] GetBytes(int value) {

     byte[] bytes = new byte[4];
     bytes[0] = (byte) ((value)&0xFF); //最低位
     bytes[1] = (byte) ((value >> 8)&0xFF);
     bytes[2] = (byte) ((value >> 16)&0xFF);
     bytes[3] = (byte) ((value >>> 24)); //最高位,无符号右移
     return bytes;
    

    }
    public static byte[] GetBytes(long values) {

     byte[] buffer = new byte[8];
     for (int i = 0; i < 8; i++) {
         int offset = 64 - (i + 1) * 8;
         buffer[i] = (byte)((values >> offset) & 0xff);
     }
     return buffer;
    

    }
    public static byte[] GetBytes(float value) {

     return GetBytes(Float.floatToIntBits(value));
    

    }
    public static byte[] GetBytes(double val) {

     long value = Double.doubleToLongBits(val);
     return GetBytes(value);
    

    }
    public static byte[] GetBytes(boolean value) {

     return new byte[]{(byte)(value? 1:0)};
    

    }

    public static byte IntToByte(int x) {

     return (byte) x;
    

    }
    public static int ByteToInt(byte b) {

     return b & 0xFF;
    

    }
    public static char ToChar(byte[] bs,int offset) {

     return (char) (((bs[offset] & 0xFF) << 8) | (bs[offset+1] & 0xFF));
    

    }
    public static byte[] GetBytes(char value) {

     byte[] b = new byte[2];
     b[0] = (byte) ((value & 0xFF00) >> 8);
     b[1] = (byte) (value & 0xFF);
     return b;
    

    }

    public static byte[] Concat(byte[]... bs) {

     int len = 0,idx=0;
     for(byte[] b:bs)len+=b.length;
     byte[] buffer = new byte[len];
     for(byte[] b:bs) {
         System.arraycopy(b,0, buffer, idx, b.length);
         idx+=b.length;
     }
     return buffer;
    

    }
    public static void main(String[] args) {

     long a = 123456;
     byte[] b1=GetBytes(a);
     long b= ToInt64(b1, 0);
     System.out.println(b);
    

    }
    }

相关文章
|
2月前
|
XML 消息中间件 人工智能
next-ai-draw-io:github开源ai绘图工具安装与使用教程
next-ai-draw-io 是一款开源AI绘图工具,深度融合 draw.io 编辑器与大语言模型(GPT/Claude等),支持自然语言生成、对话式修改、双向编辑及一键导出。保留原生体验,精准绘制复杂架构图,支持私有化部署与自动美化,大幅提升图表创作效率。(239字)
841 0
next-ai-draw-io:github开源ai绘图工具安装与使用教程
|
开发者
氚云丨开发课— 05 后端代码调试与业务对象操作| 学习笔记
快速学习氚云丨开发课— 05 后端代码调试与业务对象操作。
|
4月前
|
Unix 网络安全 数据安全/隐私保护
Windows终端工具箱mobaxterm安装使用保姆级教程
MobaXterm是Windows平台全能终端工具,集成SSH/RDP/VNC/FTP等协议,内置SFTP拖拽传输、X Server图形显示及Cygwin Unix命令支持,开箱即用。关注“贝壳资源库”回复【mobaxterm】获取。
1052 3
缓存 安全 程序员
199 0
|
1月前
|
缓存 人工智能 JSON
如何减少大模型 token 的消耗?
本文详解“省Token”本质:非弃用AI,而是精细化资源调度。涵盖Prompt优化(人设设定、格式约束、few-shot)、API工程技巧(stop参数、惩罚系数、max_tokens)及系统级方案(上下文摘要、模型路由、语义缓存),助你高效降本不降质。
356 0
|
2月前
|
机器学习/深度学习 数据挖掘 PyTorch
PyTorch深度学习实战 |手算​​FCN全卷积神经网络
本文介绍了FCN-8s语义分割网络的实现细节。首先解释了语义分割的概念及其与图像分类的区别,重点分析了FCN网络结构中的全卷积化、上采样和跳跃连接三个关键技术。全卷积化将传统CNN的全连接层改为卷积层,实现像素级分类;上采样通过双线性插值恢复特征图尺寸;跳跃连接则融合高低层特征以提升细节表现。文章详细推导了损失函数的计算过程,并提供了完整的PyTorch实现代码,包括双线性插值权重初始化、VGG16骨干网络和FCN-8s主体结构。最后通过测试验证了模型能正确输出与输入尺寸匹配的预测结果。
379 3
|
11月前
|
人工智能 自然语言处理 搜索推荐
AI营销新宠助力企业突围
AI浪潮下,企业如何借力新技术突围?OpenAI与立讯合作预示消费级AI设备爆发,AIGEO市场规模2024年将超180亿元。AI语义预检内容提升曝光效率,精准触达用户。63%网民用AI获取信息,AI搜索流量占比达42%。政策支持叠加技术进步,内容营销迎来智能变革。企业需重构策略,把握AI红利。欢迎交流咨询,共探增长新路径。
|
11月前
|
存储 机器学习/深度学习 弹性计算
阿里云服务器租用价格参考:2核4G/4核8G/8核16G价格与选型指南
阿里云服务器2核4G、4核8G、8核16G配置价格参考,目前,2核4G配置按量收费最低0.225元/小时,包年包月平均月价最低47.52元,按年购买u1实例2核4G5M带宽仅需199元且续费不涨价;4核8G配置按量收费最低降至0.45元/小时,包年包月平均月价最低159.84元;8核16G配置按量收费最低0.9元/小时,按月租用平均月价最低319.68元。云服务器实例规格和配置不同,收费标准与活动价格也不同,本文将为您介绍这三大配置的收费标准、活动价格及选型策略,以供选择参考。
|
8月前
|
人工智能 自然语言处理 语音技术
2025年AI数字人公司哪家好?数字人厂商技术产品、核心优势、应用场景对比
AI数字人迈向规模化商用,2025年呈现“技术驱动、场景分化、生态协同”趋势。涵盖服务、身份、分身三类,广泛应用于政务、医疗、文旅等领域,实现效率提升与体验升级。企业格局多元:世优科技强在全栈自研与高拟真交互,百度依托大模型赋能媒体营销,中小厂商聚焦垂直场景创新。选型需综合技术、场景、成本与生态。
542 0
|
11月前
|
机器学习/深度学习 人工智能 搜索推荐
当AI遇上癌症:聊聊个性化治疗的新可能
当AI遇上癌症:聊聊个性化治疗的新可能
354 15