Java bytestohexstring Parsing

Source: Internet
Author: User

A. Code

    /*** Convert byte[] to hex String * * In Java byte uses a binary representation of 8 bits, while 16 binary characters need to be represented by 4-bit bits, so we can convert each byte to two phases The 16 binary characters that should be converted to the 4-bit and low 4-bit byte respectively into the corresponding 16-character H and L, * and combined to get a byte converted to a 16-string result new String (H) + new String (L).     That is, byte uses hexadecimal to represent only 2 bits * * to convert byte to int, and then use integer.tohexstring (int) to convert to 16 binary strings. *     * @paramsrc byte[] Data *@returnHex String*/     Public StaticString bytestohexstring (byte[] src) {StringBuilder StringBuilder=NewStringBuilder (""); if(src = =NULL|| Src.length <= 0) {            return NULL; }         for(inti = 0; i < src.length; i++) {            intv = src[i] & 0xFF; String HV=integer.tohexstring (v); if(Hv.length () < 2) {stringbuilder.append (0);        } stringbuilder.append (HV); }        returnstringbuilder.tostring (); }

Two. Example

Note that src[i] & 0xFF takes a byte and 0xFF and then uses integer.tohexstring to get the hexadecimal string, which can be seen

src[i] & 0xFF operation is still an int, then why and how to do with 0xFF? Direct integer.tohexstring (src[i]), do you want to convert byte strong to int?

The answer is no. The reason for this is:

The size of 1.byte is 8bits and the size of int is 32bits
The 2.java binary is in complement form

Byte is a byte saved with 8 bits, which is 8 0, 1.
The first bit of a 8-bit is the sign bit,
Which means that 0000 0001 represents the number 1.
1000 0000 means-1.
So the positive maximum is 0111 1111, which is the number 127.
Negative numbers up to 1111 1111, that is, numbers-128

This is the binary source code, but in Java is used in the form of complement, the following describes what is the complement

1, anti-code:

If a number is positive, then its inverse code is the same as the original code;

If a number is negative, then the sign bit is 1, the rest of you are the original code to take the reverse;

2, Complement: Using overflow, we can change the subtraction into addition

9-4=5

Because 4+6=10, 6 as 4 of the complement, rewrite

9+6=15 (minus the high 1, which is 10) gets 5.

For hexadecimal numbers, the subtraction from C to 5 is available:
C-7=5

Because 7+9=16 9 as a 7 complement, rewrite

C+9=15 (minus the high 1, which is 16) gets 5.

⑴ A number is positive, then its original code, anti-code, the same complement

⑵ a number is negative, just the sign bit is 1, the rest of you are the original code inversion, and then the entire number plus 1

-1 of the original code is 10000001
-1 of the anti-code is 11111110
-1 of the complement is 11111111

0 of the original code is 00000000
0 of the anti-code is 11111111 (positive zero and negative zero of the same code)
0 of the complement is 100000000 (drop the beginning of 1, positive zero and the same complement of negative zero)

The integer.tohexstring parameter is int, and if you do not &0xff, then when a byte is converted to int, because int is 32 bits, and byte is only 8 bits, then the complement is

and 0xFF and after the high 24 bits will be cleared 0, the result is right.

three. hexstringtobytes

/*** Convert hex string to byte[] *@paramhexstring the hex string *@returnbyte[]*/   Public Static byte[] hexstringtobytes (String hexstring) {if(HexString = =NULL|| Hexstring.equals ("")) {           return NULL; } hexstring=hexstring.touppercase (); intLength = Hexstring.length ()/2; Char[] Hexchars =Hexstring.tochararray (); byte[] D =New byte[length];  for(inti = 0; i < length; i++) {           intpos = i * 2; D[i]= (byte) (Chartobyte (Hexchars[pos]) << 4 | chartobyte (Hexchars[pos + 1])); }       returnD; } 

Java bytestohexstring Parsing

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.