java整数和byte数组之间的转换

原文地址:http://blog.csdn.net/aotian16/article/details/9859213

记录下

[java]  view plain copy
  1. public class NumberUtil {  
  2.     /** 
  3.      * int整数转换为4字节的byte数组 
  4.      *  
  5.      * @param i 
  6.      *            整数 
  7.      * @return byte数组 
  8.      */  
  9.     public static byte[] intToByte4(int i) {  
  10.         byte[] targets = new byte[4];  
  11.         targets[3] = (byte) (i & 0xFF);  
  12.         targets[2] = (byte) (i >> 8 & 0xFF);  
  13.         targets[1] = (byte) (i >> 16 & 0xFF);  
  14.         targets[0] = (byte) (i >> 24 & 0xFF);  
  15.         return targets;  
  16.     }  
  17.   
  18.     /** 
  19.      * long整数转换为8字节的byte数组 
  20.      *  
  21.      * @param lo 
  22.      *            long整数 
  23.      * @return byte数组 
  24.      */  
  25.     public static byte[] longToByte8(long lo) {  
  26.         byte[] targets = new byte[8];  
  27.         for (int i = 0; i < 8; i++) {  
  28.             int offset = (targets.length - 1 - i) * 8;  
  29.             targets[i] = (byte) ((lo >>> offset) & 0xFF);  
  30.         }  
  31.         return targets;  
  32.     }  
  33.   
  34.     /** 
  35.      * short整数转换为2字节的byte数组 
  36.      *  
  37.      * @param s 
  38.      *            short整数 
  39.      * @return byte数组 
  40.      */  
  41.     public static byte[] unsignedShortToByte2(int s) {  
  42.         byte[] targets = new byte[2];  
  43.         targets[0] = (byte) (s >> 8 & 0xFF);  
  44.         targets[1] = (byte) (s & 0xFF);  
  45.         return targets;  
  46.     }  
  47.   
  48.     /** 
  49.      * byte数组转换为无符号short整数 
  50.      *  
  51.      * @param bytes 
  52.      *            byte数组 
  53.      * @return short整数 
  54.      */  
  55.     public static int byte2ToUnsignedShort(byte[] bytes) {  
  56.         return byte2ToUnsignedShort(bytes, 0);  
  57.     }  
  58.   
  59.     /** 
  60.      * byte数组转换为无符号short整数 
  61.      *  
  62.      * @param bytes 
  63.      *            byte数组 
  64.      * @param off 
  65.      *            开始位置 
  66.      * @return short整数 
  67.      */  
  68.     public static int byte2ToUnsignedShort(byte[] bytes, int off) {  
  69.         int high = bytes[off];  
  70.         int low = bytes[off + 1];  
  71.         return (high << 8 & 0xFF00) | (low & 0xFF);  
  72.     }  
  73.   
  74.     /** 
  75.      * byte数组转换为int整数 
  76.      *  
  77.      * @param bytes 
  78.      *            byte数组 
  79.      * @param off 
  80.      *            开始位置 
  81.      * @return int整数 
  82.      */  
  83.     public static int byte4ToInt(byte[] bytes, int off) {  
  84.         int b0 = bytes[off] & 0xFF;  
  85.         int b1 = bytes[off + 1] & 0xFF;  
  86.         int b2 = bytes[off + 2] & 0xFF;  
  87.         int b3 = bytes[off + 3] & 0xFF;  
  88.         return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;  
  89.     }  
  90. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值