java int转byte数组

int 转 byte[] 低字节在前(低字节序)
public static byte[] toLH(int n) {
byte[] b = new byte[4];
b[0] = (byte) (n & 0xff);
b[1] = (byte) (n >> 8 & 0xff);
b[2] = (byte) (n >> 16 & 0xff);
b[3] = (byte) (n >> 24 & 0xff);
return b;
}
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 int toInt(byte[] b){
int res = 0;
for(int i=0;i<b.length;i++){
res += (b[i] & 0xff) << (i*8);
}
return res;
}
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;
}

posted @   牧之丨  阅读(1880)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
历史上的今天:
2021-11-23 mybatis-plus逻辑删除不生效的解决办法
2020-11-23 回车、换行、空格的ASCII码值—(附ASCII码表)
2017-11-23 mac下用xattr命令来删除文件的扩展属性
2017-11-23 使用vue.js实现checkbox的全选和多个的删除功能
2016-11-23 【转】Popclip的JSON格式化扩展
2016-11-23 【转】json格式化、高亮库jsonFormater
2016-11-23 Mac OS X El Capitan系统完整性保护System Integrity Protection (SIP)
点击右上角即可分享
微信分享提示