Android byte[] and short[] Conversion method Code _android

Source: Internet
Author: User

1, tool code

Copy Code code as follows:

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

}

2, testing the code

Copy Code code as follows:

public class Main {

public static void Main (string[] args) {
TODO auto-generated Method Stub
Simple test of short[] turn byte[], other similar;
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]");

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.