Share Conversion between byte and the underlying type in Java

Source: Internet
Author: User

========================================================
Qiujuer
Blog: Blog.csdn.net/qiujuer
Website: www.qiujuer.net
Open Source Library: github.com/qiujuer/genius-android
Open Source Library: Github.com/qiujuer/blink
Reprint Please specify source: http://blog.csdn.net/qiujuer/article/details/45152189
--open source of learning, for open source, beginner's mentality, with June mutual encouragement!

========================================================

A long time did not write a blog, some unfamiliar, has been busy to do the Blink framework.
Advertising here, Blink is a socket cross-platform framework, now available in Java, Android, C # version, through the blink can achieve the socket transmission asynchronous encapsulation, can send or receive data directly, can now send directly byte, String, File types that extend the desired type directly without altering the frame.

Blink Open Source Framework Link

Since the transmission of the socket is Bytes transmission, it involves the conversion of the Java basic type (char, short, int, long) and byte[], summarizing it, and simply writing a class.

/** * Bit Converter * / Public  class bitconverter {    /** * Convert char to byte[] * * @param x char * @return bytes */     Public Static byte[]tobytes(Charx) {returnTobytes (x,New byte[2],0); }/** * Convert char to byte[] * * @param x char * @param bytes Dest bytes * @param bytepos Dest pos * @return bytes */     Public Static byte[]tobytes(CharXbyte[] Bytes,intBytepos) {bytes[bytepos++] = (byte) (x); Bytes[bytepos] = (byte) (x >>8);returnbytes }/** * Convert short to byte[] * * @param x Short * @return bytes */     Public Static byte[]tobytes( Shortx) {returnTobytes (x,New byte[2],0); }/** * Convert short to byte[] * * @param x Short * @param bytes Dest bytes * @param bytepos Dest pos * @return bytes */     Public Static byte[]tobytes( ShortXbyte[] Bytes,intBytepos) {bytes[bytepos++] = (byte) (x); Bytes[bytepos] = (byte) (x >>8);returnbytes }/** * Convert int to byte[] * * @param x int * @return bytes */     Public Static byte[]tobytes(intx) {returnTobytes (x,New byte[4],0); }/** * Convert int to byte[] * * @param x int * @param bytes Dest bytes * @param bytepos Dest pos * @return bytes */     Public Static byte[]tobytes(intXbyte[] Bytes,intBytepos) {bytes[bytepos++] = (byte) (x); bytes[bytepos++] = (byte) (x >>8); bytes[bytepos++] = (byte) (x >> -); Bytes[bytepos] = (byte) (x >> -);returnbytes }/** * Convert long to byte[] * * @param x long * @return bytes */     Public Static byte[]tobytes(Longx) {returnTobytes (x,New byte[8],0); }/** * Convert long to byte[] * * @param x long * @param bytes Dest bytes * @param bytepos Dest pos * @return bytes */     Public Static byte[]tobytes(LongXbyte[] Bytes,intBytepos) {bytes[bytepos++] = (byte) (x); bytes[bytepos++] = (byte) (x >>8); bytes[bytepos++] = (byte) (x >> -); bytes[bytepos++] = (byte) (x >> -); bytes[bytepos++] = (byte) (x >> +); bytes[bytepos++] = (byte) (x >> +); bytes[bytepos++] = (byte) (x >> -); Bytes[bytepos] = (byte) (x >> About);returnbytes }/** * Convert byte[] to char * * @param bytes bytes * @return Char */     Public Static Char ToChar(byte[] bytes) {returnToChar (Bytes,0); }/** * Convert byte[] to char * * @param bytes bytes * @param index byte start index * @return char * /     Public Static Char ToChar(byte[] Bytes,intIndex) {return(Char) ((Bytes[index +1] <<8) | (Bytes[index] &0xFF)); }/** * Convert byte[] to short * * @param bytes bytes * @return  Short */     Public Static  Short Toshort(byte[] bytes) {returnToshort (Bytes,0); }/** * Convert byte[] to short * * @param bytes bytes * @param index byte start index * @return  Short * *     Public Static  Short Toshort(byte[] Bytes,intIndex) {return( Short) ((Bytes[index +1] <<8) | (Bytes[index] &0xFF)); }/** * Convert byte[] to int * * @param bytes bytes * @return int */     Public Static int ToInt(byte[] bytes) {returnToInt (Bytes,0); }/** * Convert byte[] to int * * @param bytes bytes * @param Index bytes Start index * @return int * *     Public Static int ToInt(byte[] Bytes,intIndex) {return((Bytes[index +3]) << -) | ((Bytes[index +2] &0xFF) << -) | ((Bytes[index +1] &0xFF) <<8) | ((Bytes[index) &0xFF))); }/** * Convert byte[] to long * * @param bytes bytes * @return Long * *     Public Static Long Tolong(byte[] bytes) {returnTolong (Bytes,0); }/** * Convert byte[] to long * * @param bytes bytes * @param Index bytes Start index * @return long * *     Public Static Long Tolong(byte[] Bytes,intIndex) {return((((Long) Bytes[index +7]) << About) | (((Long) Bytes[index +6] &0xFF) << -) | (((Long) Bytes[index +5] &0xFF) << +) | (((Long) Bytes[index +4] &0xFF) << +) | (((Long) Bytes[index +3] &0xFF) << -) | (((Long) Bytes[index +2] &0xFF) << -) | (((Long) Bytes[index +1] &0xFF) <<8) | (((Long) Bytes[index] &0xFF))); }}

It is important to note that both the conversion from the base type to the byte[] or the byte[] to the base type are used in the form of the lower position in the former high; this is consistent with the default mode of C #, and you need to change the order if your requirements are high.

========================================================
Qiujuer
Blog: Blog.csdn.net/qiujuer
Website: www.qiujuer.net
Open Source Library: github.com/qiujuer/genius-android
Open Source Library: Github.com/qiujuer/blink
Reprint Please specify source: http://blog.csdn.net/qiujuer/article/details/45152189
--open source of learning, for open source, beginner's mentality, with June mutual encouragement!

========================================================

Share Conversion between byte and the underlying type in Java

Related Article

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.