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--) {
&nb Sp;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++) {
&nb Sp;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++) {
&nb Sp;r <<= 8;
r |= (buf[i] & 0X00000000000000FF);
}
} else {
for (int i = buf.length-1; I >= 0; i--) {
r <<= 8;
r |= (buf[i] & 0X00000000000000FF);
}
}
return R;
}
/*----------------------------------------------------------*/
/* A simple encapsulation of the conversion/*
/*----------------------------------------------------------*/
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];< br> for (int jloop = 0; jloop < blength; jloop++) {
temp[jloop] = Buf[iLoo P * 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];< br> for (int jloop = 0; jloop < blength; jloop++) {
temp[jloop] = Buf[iLoo P * 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];< br> for (int jloop = 0; jloop < blength; jloop++) {
temp[jloop] = Buf[iLoo P * 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;
}
}