Java数字类型转byte数组

2023-05-16

文章目录

    • 方法1 自己写
      • int转byte数组
      • byte数组转int
      • 参考:https://blog.csdn.net/qq_41054313/article/details/88424454
    • 方法2 使用java NIO包的功能
      • int转byte数组
      • byte数组转int
      • 参考:https://www.freesion.com/article/8678528786/
    • 方法3 使用hutool工具包
      • int转byte数组
      • byte数组转int
      • hutool依赖

方法1 自己写

int转byte数组

public static byte[] toHH(int n) {  
  byte[] b = new byte[4];  
  b[3] = (byte) (n & 0xff);  
  b[2] = (byte) (n >> 8 & 0xff);  
  b[1] = (byte) (n >> 16 & 0xff);  
  b[0] = (byte) (n >> 24 & 0xff);  
  return b;  
}

byte数组转int

public static int toInt(byte[] b){
    int res = 0;
    for(int i=0;i<b.length;i++){
        res += (b[i] & 0xff) << ((3-i)*8);
    }
    return res;
}

参考:https://blog.csdn.net/qq_41054313/article/details/88424454

方法2 使用java NIO包的功能

int转byte数组

    public static byte[] intToByteArray(int n) {
        return ByteBuffer.allocate(Integer.SIZE / Byte.SIZE).putInt(n).array();
    }

byte数组转int

    public static int byteArrayToInt(byte[] bytes) {
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.put(bytes, 0, bytes.length);
        buffer.flip();
        return buffer.getInt();
    }

参考:https://www.freesion.com/article/8678528786/

方法3 使用hutool工具包

int转byte数组

    public static byte[] intToByteArray(int n) {
        return ByteUtil.intToBytes(n, ByteOrder.BIG_ENDIAN);
    }

byte数组转int

    public static int byteArrayToInt(byte[] bytes) {
        return ByteUtil.bytesToInt(bytes,ByteOrder.BIG_ENDIAN);
    }

hutool依赖

    <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.15</version>
        </dependency>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java数字类型转byte数组 的相关文章

随机推荐

  • 2.COM接口

    2 1 再谈接口与实现 其实从上一章 COM是个更好的C 43 43 可以看出 xff0c COM最重要的就是将接口与实现分离 上一章中接口定义头文件中采用C 43 43 抽象类的形式 xff0c 如果调用方是C 43 43 环境当然不会有
  • [QT]QMessageBox 的四种用法

    之前的一些QT笔记 xff0c 整理一下 void MainWindow on info clicked info QMessageBox information this 34 Title 34 34 Text 34 void MainW
  • svn执行update,却被告知database is locked

    svn执行update xff0c 却被告知database is locked xff01 执行 svn update xff0c 却抛出个错误警报 xff1a svn E200033 database is locked executi
  • ffmpeg解码RTSP/TCP视频流H.264(QT界面显示视频画面)

    源码下载地址 http download csdn net detail liukang325 9489952 我用的ffmpeg版本为 ffmpeg 2 1 8 tar bz2 版本低了恐怕有些头文件和API找不到 在Linux下解压后编
  • PCM音频文件格式的头信息

    一个裸的PCM格式音频数据 xff0c 如果不带头信息 xff0c 不知道其采样率等相关信息 xff0c 就无法用播放器播放出来 下面是默认的头信息格式 xff1a span class hljs comment 音频头部格式 span s
  • 解决cc1plus.exe: error: out of memory allocating

    QT中增加资源文件过大时 xff0c 会编译不过 xff0c 报错 xff1a span class hljs attribute cc1plus exe span span class hljs string out of memory
  • 单片机 APROM: RAM: Flash:区别

    APROM是用户程序存储区 xff0c 我们写的单片机的程序的代码 xff0c 就放在这里 APROM是 xff0c APROM是Flash中的一部分 RAM xff0c 随机存储器 xff0c 主要用来存放动态数据 xff0c 比如我们程
  • 改变全局变量值得两种方法

    方法一 xff1a 指针法 include lt iostream gt using namespace std void change int a void main int t change amp t 注意这里是传入变量的地址 xff
  • QT中为程序加入超级管理员权限

    QT的一些文件操作 xff0c 注册表的操作等 xff0c 有些操作会无效 xff0c 主要是因为没有对C盘的相关权限 解决方法 xff1a 1 mingw编译器 在pro工程文件中加入 span class hljs attribute
  • QT截图非顶层窗口的画面(获取窗口句柄)

    我们知道QT里截图的代码很简单 xff0c 很多例子都是截取桌面 xff0c 或截取整个屏幕 那如果要截取指定窗口的画面呢 xff1f 即使该窗口不在桌面最顶层显示 我们也能截到它的图片吗 xff1f 当然可以 xff0c 只要我们拿到该窗
  • QEventLoop会卡住的解决方法

    问题是这样的 xff1a 在一个线程中有下面一段代码 QEventLoop span class hljs keyword loop span span class hljs comment span span class hljs lab
  • android adb 模拟点击、滑动、输入、按键

    模拟输入 001 adb shell input text 001 模拟home按键 adb shell input keyevent 3 模拟点击 540 1104 坐标 adb shell input tap 540 1104 模拟滑动
  • 结构体在内存中的对齐规则

    一个结构体变量定义完之后 xff0c 其在内存中的存储并不等于其所包含元素的宽度之和 例一 xff1a include lt iostream gt using namespace std struct X char a
  • curl请求常用参数和返回码

    curl是一个用于传输数据的工具 xff0c 支持各种协议 xff0c 如HTTP FTP SMTP等 以下是一些常用的curl请求参数及其作用 xff1a X request xff1a 指定HTTP请求方法 xff0c 常见的有GET
  • ubuntu中python版本切换

    shell里执行 xff1a sudo update alternatives install usr bin python python usr bin python2 100 sudo update alternatives insta
  • CMAKE基础使用

    1 目录结构 xff1a 2 顶层cmake内容 xff1a span class token function cmake minimum required span span class token punctuation span V
  • URL格式

    一 URL基本格式 一个完整的url包含方案 用户名 密码 主机名 端口 路径 参数 查询和片段 xff0c 格式如下 xff1a lt scheme gt lt user gt lt password gt 64 lt host gt l
  • __IO uint16_t

    STM32里的类型定义 xff0c 见如下说明 xff1a typedef volatile unsigned short vu16 typedef IO uint16 t vu16 IO definitions access restri
  • 串口波形分析(TTL,RS232,RS485)

    TTL xff0c RS232 xff0c RS485波形分析 本文转自 xff1a http blog 163 com qiu zhi2008 blog static 60140977201092651854445 http www cn
  • Java数字类型转byte数组

    文章目录 方法1 自己写int转byte数组byte数组转int参考 xff1a https blog csdn net qq 41054313 article details 88424454 方法2 使用java NIO包的功能int转