上下求索

【原创】Java中Byte字节流处理的自定义方法库2

上一篇 / 下一篇  2010-07-13 09:52:23

1.把整型转换成指定长度的数组字节流
 public static byte[] int2bytes(int integer, int len) {
 //   if (integer < 0) { throw new IllegalArgumentException("Can not cast negative to bytes : " + integer); }
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    for (int i = 0; i < len; i++) {
     bo.write(integer);
     integer = integer >> 8;
    }
    byte[] res=reversEndian(bo.toByteArray(),len);
    return res;
 }

private static byte[] reversEndian(byte str[],int len) {
        byte b;
        byte res[]=new byte[len];
            for(int i=0;i<len;i++)
            {
               b=str[i];
               res[len-i-1]=b;            
            }
        return res;
     }

2.把整型转换成TLV方式的字节数组流
 public static byte[] int2TLVbytes(int tag,int value,int len) throws IOException{
  byte[] tag1=int2bytes(tag,4);
  byte[] length1=int2bytes(len,4);
  byte[] value1=int2bytes(value,len);
  byte[] buff=pack(tag1,length1,value1);
  return buff;
 }
3.把UTF-8的string转换成指定长度的字节数组流,不足部分补0x00
 public static byte[] UTF8string2bytes(String source, int length,byte fillByte) {
  byte[] dst = new byte[length];
  if (source == null) {
   for (int i = 0; i < length; i++) {
    dst[i] = fillByte;
   }
   return dst;
  }
  try {
   byte[] b = source.getBytes("UTF-8");
   if (b.length > length) {
    System.arraycopy(b, 0, dst, 0, length);
   } else {
    System.arraycopy(b, 0, dst, 0, b.length);
    for (int i = dst.length; i < length; i++) {
     dst[i] = fillByte;
    }
   }
  } catch (Exception e) {
   for (int i = 0; i < length; i++) {
    dst[i] = fillByte;
   }
  }
  return dst;
 }
4.把string转换成TLV编码方式的字节数组流
 public static byte[] UTF8string2TLVbytes(int tag,String value){
   int length=value.length();
   byte[] tag1=int2bytes(tag,4);
   byte[] length1=int2bytes(length,4);
   byte[] value1 = null;
   try {
    value1 = UTF8string2bytes(value,value.getBytes("UTF-8").length,(byte)0x00);
   } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   byte[] buff=pack(tag1,length1,value1);
   return buff;
 }
5.把字节流转换成UTF-8字符串
 public static String bytes2UTF8string(byte source[]) {
  String dst = "";
  try {
   dst = (new String(source, "UTF-8"));
  } catch (UnsupportedEncodingException e) {
   dst = "";
  }
  return dst;
 }
6.将字节流中的指定字节段转换成UTF-8字符型
 public static String bytes2UTF8string2(byte b[],int offset,int len){
  byte[] a=new byte[len];
  for (int i=0;i<len;i++){
   a[i]=b[offset];
   offset++;
  }
  return bytes2UTF8string(a);
 }
7.字节流数据转换成整型
 public static int bytes2int(byte b[]) {
  int s = 0;
  for (int i = 0; i < b.length; i++) {
   s = s | ((b[i] & 0xff) << ((b.length - i - 1) * 8));
  }
  return s;
 }
8.将字节流中的指定字节段转换成整型
 public static int bytes2int2(byte b[],int offset,int len){
  byte[] respcode=new byte[len];
  for (int i=0;i<len;i++){
   respcode[i]=b[offset];
   offset++;
  }
  return bytes2int(respcode);
 }

其中部分引用Java中Byte字节流处理的自定义方法库1中设计的方法。


TAG: java Java JAVA

 

评分:0

我来说两句

我的栏目

日历

« 2024-04-17  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 126657
  • 日志数: 65
  • 建立时间: 2009-06-24
  • 更新时间: 2013-11-01

RSS订阅

Open Toolbar