Java byte data type conversion __java

Source: Internet
Author: User
Reprint Address: http://www.cnblogs.com/xyqCreator/archive/2012/06/13/2547657.html
The public class Datatypechangehelper {/** * Converts a single-byte byte to a 32-bit int * * @param b * Byte @return Conve
	RT result */public static int Unsignedbytetoint (byte b) {return (int) B & 0xFF; /** * Converts a single-byte byte to a number of 16 binary * * @param b * byte * @return convert result/public static stri
		ng Bytetohex (byte b) {int i = b & 0xFF;
	return integer.tohexstring (i); /** * Converts a 4byte array to a 32-bit int * * @param buf * bytes buffer * @param byte[] Start the conversion in the position * @return Co
		Nvert result */public static long Unsigned4bytestoint (byte[] buf, int pos) {int firstbyte = 0;
		int secondbyte = 0;
		int thirdbyte = 0;
		int fourthbyte = 0;
		int index = POS;
		Firstbyte = (0X000000FF & ((int) buf[index]));
		Secondbyte = (0X000000FF & ((int) Buf[index + 1]));
		Thirdbyte = (0X000000FF & ((int) Buf[index + 2]));
		Fourthbyte = (0X000000FF & ((int) Buf[index + 3]));
		Index = index + 4; Return ((Long) (Firstbyte<< 24 | Secondbyte << 16 | Thirdbyte << 8 |
	fourthbyte)) & 0xFFFFFFFFL; /** * Converts a 16-bit short to a byte array * * @param s * short * @return byte[] length is 2 * * */public static byte[
		] Shorttobytearray (short s) {byte[] targets = new BYTE[2];
			for (int i = 0; i < 2; i++) {int offset = (Targets.length-1-i) * 8;
		Targets[i] = (byte) ((S >>> offset) & 0xff);
	return targets; /** * Converts a 32-bit integer to a byte array of length 4 * * @param s * int * @return byte[] * */public static byte[] Intt
		Obytearray (int s) {byte[] targets = new BYTE[2];
			for (int i = 0; i < 4; i++) {int offset = (Targets.length-1-i) * 8;
		Targets[i] = (byte) ((S >>> offset) & 0xff);
	return targets; /** * Long to byte[] * * @param s * long * @return byte[] * * */public static byte[] Longtobyt
		Earray (long s) {byte[] targets = new BYTE[2]; for (int i = 0; i < 8; i++) {int offsET = (TARGETS.LENGTH-1-i) * 8;
		Targets[i] = (byte) ((S >>> offset) & 0xff);
	return targets;
		}/** 32-bit int byte[] * */public static byte[] Int2byte (int res) {byte[] targets = new BYTE[4]; Targets[0] = (byte) (res & 0xff);//lowest bit targets[1] = (byte) ((res >> 8) & 0xff);//secondary low targets[2] = (byt
		e) ((res >>) & 0xff);//secondary high targets[3] = (byte) (res >>> 24);//highest bit, unsigned right shift.
	return targets; /** * Converts a byte array of length 2 to a 16-bit int * * @param res * byte[] * @return int */public static int byte
		2int (byte[] res) {/Res = Inversionbyte (res); One byte data left 24 digits to 0x?? 000000, and then move to the right 8 to become 0x00?? 0000 int targets = (Res[0] & 0xff) | ((Res[1] << 8) & 0xff00); // |
	Indicates an targets or return; }
}

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.