android中byte[] short[] int[] long[]数组数据转换

import java.nio.ByteOrder;

public class BytesTransUtils {

	 private String TAG = "BytesTransUtils";
	 private static BytesTransUtils instance = null;

	 private BytesTransUtils() {
	  // Log.i(TAG, "instance BytesTransUtils");
	 }

	 public static BytesTransUtils getInstance() {
		  if (instance == null) {
			  instance = new BytesTransUtils();
		  }

		  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;
	 }

}
时间: 2024-11-05 16:09:30

android中byte[] short[] int[] long[]数组数据转换的相关文章

Android中TextView setText int 报错

在对中TextView setText 覆值int 时报错,网上查下原因是setText整型表明是设值R.id.xxx,当然找不到. 解决方法是将int转化为string,用String.valueOf(xxx) Android中TextView setText int 报错,布布扣,bubuko.com

java中byte与int互转

package com.yl.common.utils; /** * byte转换工具 * * @author huangzp * @date 2015-6-09 */ public class ByteUtil { /** * 将iSource转为长度为iArrayLen的byte数组,字节数组的低位是整型的低字节位 * @param iSource * @param iArrayLen * @return */ public static byte[] toByteArray(int iSo

关于Java中byte,short,char,int 之间相互赋值的问题

首先明确这几种数据类的取值范围: byte:  -128~127 short: -2^15~2^15-1 char: 0~65536 int: -2^31~2^31-1 请看以下代码: byte b = 100; short s = b; //正确,因为byte的取值范围在short取值范围之内. char c = b; //错误,因为byte的取值范围不完全在char的取值范围内. c = s;  //错误,因为short的取值范围不完全在char的取值范围内. int x = b; //正确

Java中byte转int的方法

byte转化为int有两种情况: 1)要保持数值不变 应用场景:数值计算.等等. 方法:能够直接採用强制类型转换:int i = (int) aByte, 比如:若aByte=0xff(即数值为-1).则转化为int后.i为0xffffffff.数值仍为-1. 2)保持最低字节中各个位不变,3个高字节所实用0填充 应用场景:编解码操作, 方法:採用位操作:int i = aByte & 0xff. 比如:若aByte=0xff,转化为int后.i为0x000000ff.

java中byte转换int时为何与0xff进行与运算

在剖析该问题前请看如下代码 public static String bytes2HexString(byte[] b) {  String ret = "";  for (int i = 0; i < b.length; i++) {   String hex = Integer.toHexString(b[i] & 0xFF);   if (hex.length() == 1) {    hex = ''0'' + hex;   }   ret += hex.toUp

java中byte转换int时为何与0xff进行与运算(转)

在剖析该问题前请看如下代码 Java代码   public static String bytes2HexString(byte[] b) { String ret = ""; for (int i = 0; i < b.length; i++) { String hex = Integer.toHexString(b[ i ] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } ret += hex.toUpperC

java整型byte,short,int,long取值范围大小

 byte 1个字节 short 2个字节 int 4个字节long 8 个字节 varchar 可变长度的非Unicode数据,最长为8000个字符nvarchar 可变长度Unicode数据,最长为4000个字符char 固定长度的非Unicode数据,最长为8000个字符nchar 固定长度的Unicode数据,最长为4000个字符 非Unicode字符串的数据类型 包括char ,varchar,text; Unicode字符串的数据类型 包括nchar ,nvarchar,ntext;

[转]java中byte转换int时为何与0xff进行与运算

在剖析该问题前请看如下代码public static String bytes2HexString(byte[] b) {  String ret = "";  for (int i = 0; i < b.length; i++) {   String hex = Integer.toHexString(b[ i ] & 0xFF);   if (hex.length() == 1) {    hex = '0' + hex;   }   ret += hex.toUpp

Java中char,short,int,long占几个字节和多少位

1.字节:byte:用来计量存储容量的一种计量单位:位:bit 2.一个字节等于8位  1byte = 8bit char占用的是2个字节 16位,所以一个char类型的可以存储一个汉字. 整型: byte:1个字节 8位 -128~127 short :2个字节 16位 int :4个字节 32位 long:8个字节 64位 浮点型: float:4个字节 32 位 double :8个字节 64位 注:默认的是double类型,如3.14是double类型的,加后缀F(3.14F)则为flo