Android byte[] short[] int[] long[] Array Data Conversion

Source: Internet
Author: User
<span id="Label3"></p><pre class="java" name="code">Import Java.nio.byteorder;public class Bytestransutils {private String TAG = "bytestransutils"; private static Bytestrans Utils instance = null; Private Bytestransutils () {//log.i (TAG, "instance bytestransutils");} public static bytestransutils getinstance () {i F (instance = = Null) {instance = new Bytestransutils (); } return instance; } public boolean testcpu () {if (byteorder.nativeorder () = = Byteorder.big_endian) {//System.out.println ("is BIG Endin G "); 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 ar Ray 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 Arra Y 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; }/*----------------------------------------------------------*/* make a simple package for the conversion */*------------------------------ ----------------------------*/public byte[] getBytes (int. i) {return getBytes (i, this.testcpu ());} public byte[] getbyte s (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 ());}/****************************************/pub Lic 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-&Gt; " +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; }}</pre><p><p><br></p></p><p><p>Android byte[] short[] int[] long[] Array Data Conversion</p></p></span>

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.