欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android byte[] 和short[]转换的方法代码

程序员文章站 2023-11-14 08:17:16
1,工具代码复制代码 代码如下:public class bytestransutil {  private string tag = "bytestransu...

1,工具代码

复制代码 代码如下:

public class bytestransutil {

 private string tag = "bytestransutil";
 private static bytestransutil instance = null;

 private bytestransutil() {
  // log.i(tag, "instance bytestransutil");
 }

 public static bytestransutil getinstance() {
  if (instance == null) {
   instance = new bytestransutil();
  }
  return instance;
 }

 public boolean testcpu() {
  if (byteorder.nativeorder() == byteorder.big_endian) {
   // system.out.println("is big ending");
   return true;
  } else {
   // system.out.println("is little ending");
   return false;
  }
 }

 public byte[] getbytes(short s, boolean bbigending) {
  byte[] buf = new byte[2];
  if (bbigending)
   for (int i = buf.length - 1; i >= 0; i--) {
    buf[i] = (byte) (s & 0x00ff);
    s >>= 8;
   }
  else
   for (int i = 0; i < buf.length; i++) {
    buf[i] = (byte) (s & 0x00ff);
    s >>= 8;
   }
  return buf;
 }

 public byte[] getbytes(int s, boolean bbigending) {
  byte[] buf = new byte[4];
  if (bbigending) {
   for (int i = buf.length - 1; i >= 0; i--) {
    buf[i] = (byte) (s & 0x000000ff);
    s >>= 8;
   }
  } else {
   system.out.println("1");
   for (int i = 0; i < buf.length; i++) {
    buf[i] = (byte) (s & 0x000000ff);
    s >>= 8;
   }
  }
  return buf;
 }

 public byte[] getbytes(long s, boolean bbigending) {
  byte[] buf = new byte[8];
  if (bbigending)
   for (int i = buf.length - 1; i >= 0; i--) {
    buf[i] = (byte) (s & 0x00000000000000ff);
    s >>= 8;
   }
  else
   for (int i = 0; i < buf.length; i++) {
    buf[i] = (byte) (s & 0x00000000000000ff);
    s >>= 8;
   }
  return buf;
 }

 public short getshort(byte[] buf, boolean bbigending) {
  if (buf == null) {
   throw new illegalargumentexception("byte array is null!");
  }
  if (buf.length > 2) {
   throw new illegalargumentexception("byte array size > 2 !");
  }
  short r = 0;
  if (bbigending) {
   for (int i = 0; i < buf.length; i++) {
    r <<= 8;
    r |= (buf[i] & 0x00ff);
   }
  } else {
   for (int i = buf.length - 1; i >= 0; i--) {
    r <<= 8;
    r |= (buf[i] & 0x00ff);
   }
  }

  return r;
 }

 public int getint(byte[] buf, boolean bbigending) {
  if (buf == null) {
   throw new illegalargumentexception("byte array is null!");
  }
  if (buf.length > 4) {
   throw new illegalargumentexception("byte array size > 4 !");
  }
  int r = 0;
  if (bbigending) {
   for (int i = 0; i < buf.length; i++) {
    r <<= 8;
    r |= (buf[i] & 0x000000ff);
   }
  } else {
   for (int i = buf.length - 1; i >= 0; i--) {
    r <<= 8;
    r |= (buf[i] & 0x000000ff);
   }
  }
  return r;
 }

 public long getlong(byte[] buf, boolean bbigending) {
  if (buf == null) {
   throw new illegalargumentexception("byte array is null!");
  }
  if (buf.length > 8) {
   throw new illegalargumentexception("byte array size > 8 !");
  }
  long r = 0;
  if (bbigending) {
   for (int i = 0; i < buf.length; i++) {
    r <<= 8;
    r |= (buf[i] & 0x00000000000000ff);
   }
  } else {
   for (int i = buf.length - 1; i >= 0; i--) {
    r <<= 8;
    r |= (buf[i] & 0x00000000000000ff);
   }
  }
  return r;
 }

 /*----------------------------------------------------------*/
 /* 对转换进行一个简单的封装 */
 /*----------------------------------------------------------*/
 public byte[] getbytes(int i) {
  return getbytes(i, this.testcpu());
 }

 public byte[] getbytes(short s) {
  return getbytes(s, this.testcpu());
 }

 public byte[] getbytes(long l) {
  return getbytes(l, this.testcpu());
 }

 public int getint(byte[] buf) {
  return getint(buf, this.testcpu());
 }

 public short getshort(byte[] buf) {
  return getshort(buf, this.testcpu());
 }

 public long getlong(byte[] buf) {
  return getlong(buf, this.testcpu());
 }

 /****************************************/
 public short[] bytes2shorts(byte[] buf) {
  byte blength = 2;
  short[] s = new short[buf.length / blength];
  for (int iloop = 0; iloop < s.length; iloop++) {
   byte[] temp = new byte[blength];
   for (int jloop = 0; jloop < blength; jloop++) {
    temp[jloop] = buf[iloop * blength + jloop];
   }
   s[iloop] = getshort(temp);
  }
  return s;
 }

 public byte[] shorts2bytes(short[] s) {
  byte blength = 2;
  byte[] buf = new byte[s.length * blength];
  for (int iloop = 0; iloop < s.length; iloop++) {
   byte[] temp = getbytes(s[iloop]);
   for (int jloop = 0; jloop < blength; jloop++) {
    buf[iloop * blength + jloop] = temp[jloop];
   }
  }
  return buf;
 }

 /****************************************/
 public int[] bytes2ints(byte[] buf) {
  byte blength = 4;
  int[] s = new int[buf.length / blength];
  for (int iloop = 0; iloop < s.length; iloop++) {
   byte[] temp = new byte[blength];
   for (int jloop = 0; jloop < blength; jloop++) {
    temp[jloop] = buf[iloop * blength + jloop];
   }
   s[iloop] = getint(temp);
   system.out.println("2out->"+s[iloop]);
  }
  return s;
 }

 public byte[] ints2bytes(int[] s) {
  byte blength = 4;
  byte[] buf = new byte[s.length * blength];
  for (int iloop = 0; iloop < s.length; iloop++) {
   byte[] temp = getbytes(s[iloop]);
   system.out.println("1out->"+s[iloop]);
   for (int jloop = 0; jloop < blength; jloop++) {
    buf[iloop * blength + jloop] = temp[jloop];
   }
  }
  return buf;
 }

 /****************************************/
 public long[] bytes2longs(byte[] buf) {
  byte blength = 8;
  long[] s = new long[buf.length / blength];
  for (int iloop = 0; iloop < s.length; iloop++) {
   byte[] temp = new byte[blength];
   for (int jloop = 0; jloop < blength; jloop++) {
    temp[jloop] = buf[iloop * blength + jloop];
   }
   s[iloop] = getlong(temp);
  }
  return s;
 }

 public byte[] longs2bytes(long[] s) {
  byte blength = 8;
  byte[] buf = new byte[s.length * blength];
  for (int iloop = 0; iloop < s.length; iloop++) {
   byte[] temp = getbytes(s[iloop]);
   for (int jloop = 0; jloop < blength; jloop++) {
    buf[iloop * blength + jloop] = temp[jloop];
   }
  }
  return buf;
 }

}

2,测试代码

复制代码 代码如下:

public class main {

 public static void main(string[] args) {
  // todo auto-generated method stub
  //简单测试了short[] 转byte[],其他类似;
  bytestransutil bytestransutil = bytestransutil.getinstance();
  int[] stest = { 12345678, 87654321 };
  byte[] byteshort = bytestransutil.ints2bytes(stest);
  int[] stemp = bytestransutil.bytes2ints(byteshort);
  system.out.println("short[0] = " + stemp[0] + "   short[1] = " + stemp[1]);

 }
}