Conversion Between Binary hexadecimal and byte arrays in Java

Source: Internet
Author: User

First, let's take a look at the bytebuffer abstract class. Generally, the communication format between the server and the client is fixed and both have the message header and body, that is, the custom communication protocol (both based on the TCP/IP layer). Each message header and message body have a fixed attribute (for example, the message header has 20 bytes, the first 10 bytes indicate the message length, and the last 10 bytes indicate the message type. The message length and message type are called attributes) each communication message between the client and the server is sent to each other in such a fixed format. Since their format length is fixed, what if the length is not enough? The server and client must be supplemented with placeholders for mutual understanding. Bytebuffer the byte buffer is an inheritance and buffer, which is very convenient when filling the byte array. Below is a small example:

Assume that a password is entered. I don't know the number of bytes, but it must be 21 bytes full:

/** Fill the password with 21 bytes */Public byte [] Password () {string Password = "11111111111111111111"; bytebuffer password_byte = bytebuffer. allocate (21); byte [] buf_password = password. getbytes (); bytebuffer T = bytebuffer. allocate (1); If (buf_password.length <21) {password_byte.put (buf_password); For (INT I = buf_password.length; I <21; I ++) {password_byte.put (T. array () ;}} else {password_byte.put (buf_password);} return password_byte.array ();}

It returns a 21-byte array. I used NULL bytes to fill. Bytebuffer T = bytebuffer. allocate (1); password_byte.put (T. array (); first declare a byte buffer with a length of 1, and then fill in the byte array it returns to the previously declared byte buffer.

Because the communication format between the client and the server is fixed, some attributes are also fixed. For example, when I log on to the server and send the password and user name, the server returns a message to me, in this message, an attribute is defined. For example, 0x11 indicates that the logon is successful, and 0x10 indicates that the logon fails. You must convert the returned byte to a hexadecimal number or a 10-digit number and compare it with the pre-defined data area to determine whether the login is successful or fails. Next we will introduce several hexadecimal conversions (also refer to online examples. If there are similarities, please forgive me !)

Convert byte array to integer:

public int byte_Integer(byte[] str){int result = ((((str[0] & 0xff) << 8 | (str[1] & 0xff)) << 8) | (str[2] & 0xff)) << 8    | (str[3] & 0xff);return result;}

Convert integer to byte array:

Method 1:

public byte[] Integer_byte(int num){byte[] result = new byte[4];result[0] =(byte)((num>>24) &0xff);result[1] =(byte)((num>>16) &0xff);result[2] =(byte)((num>>8) &0xff);result[3] =(byte)((num>>0) &0xff);return result;}

Method 2:

public byte[] Integer_byte(int num){ByteBuffer buff=ByteBuffer.allocate(4);buff.putInt(num);byte []bytes=buff.array();return bytes;}

Method 1 note that you should first move the first high to the low position, if it is: num> 0, num> 8 .. then, put the obtained byte array into the previous byte array and convert it into an integer function. Then, the original num is not obtained.

Method 2: Use the putint () function in bytebuffer to convert an integer to a binary array.

The hexadecimal integer and the octal integer byte array are converted as above.

Character conversion to byte array:

public byte[] String_byte(String str){byte [] result = str.getBytes();return result;}

Actually, the getbytes () function in the string class is used to return the byte array of the character.

Convert byte arrays to characters:

public String byte_String(byte[] str){char [] c = new char[]{};for(int i=0;i<str.length;i++){c[i] = (char)str[i];}String buffer = new String(c);return buffer;}

In fact, the bytes are forcibly converted to the cha type and saved in a cha array. Then, the entire Cha array is converted to a string. Note: The English and Chinese characters cannot be mixed here, because one letter occupies one byte and two Chinese characters occupy two bytes. This method is strongly converted by one byte, therefore, it is impossible to transfer the strong Chinese characters out.

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.