Chinaunix首页 | 论坛 | 博客
  • 博客访问: 160886
  • 博文数量: 56
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 593
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-18 09:59
文章分类

全部博文(56)

文章存档

2019年(1)

2018年(26)

2016年(1)

2015年(6)

2014年(22)

我的朋友

分类: Java

2014-02-18 10:06:47


点击(此处)折叠或打开

  1. public class ByteUtils {

  2.     /**
  3.      * @author:      吴永行
  4.      * @dateTime:     2014-1-20 下午9:56:00
  5.      * @description:     
  6.      *
  7.      */
  8.     public static int bytes2Int(byte[] b, int start, int len) {
  9.         int sum = 0;
  10.         int end = start + len;
  11.         for (int i = start; i < end; i++) {
  12.             int n = ((int) b[i]) & 0xff;
  13.             n <<= (--len) * 8;
  14.             sum = sum + n;
  15.         }
  16.         return sum;
  17.     }

  18.     /**
  19.      * @author:      吴永行
  20.      * @dateTime:     2014-1-20 下午10:01:00
  21.      * @description:     
  22.      *
  23.      */
  24.     public static String bytes2String(byte[] b, int start, int len) {
  25.     
  26.      return new String(b,start,len);
  27.     }

  28.     /**
  29.      * @author:      吴永行
  30.      * @dateTime:     2014-1-20 下午10:02:46
  31.      * @description:     
  32.      * @param newStr
  33.      * @return
  34.      *
  35.      */
  36.     public static byte[] string2Bytes(String str) {
  37.     
  38.      return str.getBytes();
  39.     }

  40.     /**
  41.      * @author:      吴永行
  42.      * @dateTime:     2014-1-20 下午10:03:43
  43.      * @description:     
  44.      * @param length
  45.      * @param u2
  46.      * @return
  47.      *
  48.      */
  49.     public static byte[] int2Bytes(int value, int len) {
  50.      byte[] b = new byte[len];
  51.      for (int i = 0; i < len; i++) {
  52.             b[len-i-1] = (byte) ((value >> 8 * i) & 0xff);
  53.         }
  54.      return b;
  55.     }

  56.     /**
  57.      * @author:      吴永行
  58.      * @dateTime:     2014-1-20 下午10:06:19
  59.      * @description:     
  60.      * @param classByte
  61.      * @param i
  62.      * @param u2
  63.      * @param strLen
  64.      * @return
  65.      *
  66.      */
  67.     public static byte[] bytesReplace(byte[] originalBytes, int offset, int len,
  68.  byte[] replaceBytes) {
  69.         byte[] newBytes = new byte[originalBytes.length
  70.          + (replaceBytes.length - len)];
  71.         System.arraycopy(originalBytes, 0, newBytes, 0, offset);
  72.         System.arraycopy(replaceBytes, 0, newBytes, offset, replaceBytes.length);
  73.         System.arraycopy(originalBytes, offset + len, newBytes, offset
  74.          + replaceBytes.length, originalBytes.length - offset - len);
  75.         return newBytes;
  76.     }

  77. }

阅读(959) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

啦哆A梦2014-02-19 10:38:10