android byte[] 和short[]的转换

来源:互联网 发布:艾瑞社交数据研究报告 编辑:程序博客网 时间:2024/04/30 22:46
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;}elsefor (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;}elsefor (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;}}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 非洲菊生虫子了怎么办 多肉的花剪下来怎么办 结石痛怎么办怎么止疼 喝玫瑰花茶胃疼怎么办 卡地亚戒指掉色怎么办 苹果8plus掉电快怎么办 苹果8plus耗电快怎么办 卡地亚戒指划痕怎么办 苹果手机玫瑰金掉色怎么办 14k玫瑰金掉色怎么办 卡地亚手镯掉色怎么办 手机掉油漆里了怎么办 黄金戴久了变黑怎么办 玫瑰金褪色不亮了怎么办 黄金带久了不亮怎么办 玉石带久了不亮怎么办 手表带久了不亮怎么办 蜜蜡带久了不亮怎么办 钛钢首饰不亮了怎么办 潘多拉玫瑰金戒指褪色了怎么办 金色手表漆掉了怎么办 玫瑰金表带褪色后怎么办 K金褪色或泛黄怎么办 钛钢玫瑰金变黑怎么办 玫瑰金手镯掉色了怎么办 彩金颜色不亮了怎么办 玫瑰金链子黑了怎么办 18k玫瑰金变黑了怎么办 玫瑰金戒指遇到火变黑怎么办 18k白金发黄了怎么办 18k金掉色后怎么办吗 dw手表金色掉漆怎么办 dw玫瑰金手表褪色怎么办 dw金色表带黑了怎么办 机械表机芯坏了怎么办 银镀玫瑰金褪色怎么办 苹果七p玫瑰金掉漆怎么办 美度镀金表掉色怎么办 吃了发黑的香菇怎么办 脸上的皮肤暗黄怎么办 吃了变黑的香菇怎么办