Java Size End Conversion

Source: Internet
Author: User

Tag:har   public   count    Array    unicode    int   for   byte   class   

Package nlp.nlp;/** * Small end data, byte conversion * */public class Byteconvert {public static void main (string[] args) {Byteco        Nvert C = new Byteconvert ();    C.int2bytes_le (126);    } public static final int unicode_len = 2;        /** * int is converted to small end byte[] (high position placed in higher address) * @param ivalue * @return */public byte[] Int2bytes_le (int ivalue) {        byte[] rst = new BYTE[4];        First write the last byte of int rst[0] = (byte) (Ivalue & 0xFF);        int penultimate byte rst[1] = (byte) ((Ivalue & 0xff00) >> 8);        int penultimate byte rst[2] = (byte) ((Ivalue & 0xFF0000) >> 16);        int first byte rst[3] = (byte) ((Ivalue & 0xff000000) >> 24);    return rst;        }/** * Convert string to byte[] * @param str * @return */public byte[] String2bytes_le (string str) {        if (str = = null) {return null;         } char[] chars = Str.tochararray ();         byte[] rst = Chars2bytes_le (chars);  return rst;  }/** * Convert character array to fixed length byte[] * @param chars character array * @return returns a byte array if the specified length is not sufficient to return NULL */P        Ublic byte[] Chars2bytes_le (char[] chars) {if (chars = = null) return null;                int icharcount = Chars.length;        byte[] rst = new Byte[icharcount*unicode_len];        int i = 0;            for (i = 0; i < Icharcount; i++) {rst[i*2] = (byte) (Chars[i] & 0xFF);        Rst[i*2 + 1] = (byte) ((Chars[i] & 0xff00) >> 8);    } return rst; }/** * Convert byte array to int (small end) * @return * @note array length is at least 4, converted by small end, that is, the incoming bytes is small, organized into INT */public int by this Law        Bytes2int_le (byte[] bytes) {if (Bytes.length < 4) return-1;        int iRst = (Bytes[0] & 0xFF);        IRst |= (bytes[1] & 0xFF) << 8;        IRst |= (bytes[2] & 0xFF) << 16;        IRst |= (bytes[3] & 0xFF) << 24;    return iRst; }/** * Convert byte array to int (big-endian) * @return * @note array length is at least 4, converted by small end, that is, the incoming bytes is big-endian, organized into INT */public int bytes2int_be (byte[] bytes) {if (bytes.length        < 4) return-1;        int iRst = (Bytes[0] <<) & 0xFF;        IRst |= (bytes[1] <<) & 0xFF;        IRst |= (bytes[2] << 8) & 0xFF;        IRst |= bytes[3] & 0xFF;    return iRst; }/** * Convert byte array to char (small end) * @return * @note array length is at least 2, converted by small end */public char bytes2char_le (byte[] Byt        ES) {if (Bytes.length < 2) return (char)-1;        int iRst = (Bytes[0] & 0xFF);                IRst |= (bytes[1] & 0xFF) << 8;    Return (char) iRst; }/** * Convert byte array to char (big-endian) * @return * @note array length is at least 2, converted by small end */public char bytes2char_be (byte[] Byt        ES) {if (Bytes.length < 2) return (char)-1;        int iRst = (Bytes[0] << 8) & 0xFF;        IRst |= bytes[1] & 0xFF;    Return (char) iRst; }}

  

Java Size End Conversion

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.