On hex Coding algorithm

Source: Internet
Author: User
Tags base64

First, what is hex

The hexadecimal representation of each byte represents the content, displayed in a string.

Second, the role

Converts an invisible, complex array of bytes data into a string data that can be displayed

Similar to BASE64 encoding algorithm

Difference: Base64 converts three bytes to four characters, Hex converts three bytes to six bytes

Third, the application scenario

Use when you include invisible data (binary data) in text such as Xml,json

Iv. Use of

1. Converting a byte array to a string

1 /**2 * Converts a byte array to a string3 * One byte will form two characters, the final length is twice times the original data4 * @param data5 * @return6      */7      Public StaticString Tohex (byte[] data) {8String ret =NULL;9 Ten         //TODO converts a byte array to a string One         if(Data! =NULL&& data.length>0) { AStringBuilder SB =NewStringBuilder (); -              for(byteb:data) { -                 //get four-bit, four-bit low content, and convert two values to characters the                 intH = (b>>4) &0x0f; -                 intL = b&0x0f; -                 Charch, cl; -                 if(H >9 ){ +CH = (Char) ('A'+ (H-Ten)); -}Else{//0--9 +CH = (Char) ('0'+h); A                 } at  -                 if(l>9){ -CL = (Char) ('A'+ (L-Ten)); -}Else{//0--9 -CL = (Char) ('0'+l); -                 } in  -  to sb.append (CH). Append (CL); +                 } -RET =sb.tostring (); the             } *  $         returnret;Panax Notoginseng}

2. Converting a string to a byte array

1   Public Static byte[] Fromhex (String str) {2         byte[] ret =NULL;3 4         //TODO restores hex-encoded string to original byte array5         if(str! =NULL) {6             intLen =str.length ();7             if(Len >0&& len%2==0) {8                 Char[] CHS =Str.tochararray ();9RET =New byte[Len/2];Ten                  for(inti =0, j =0; i < Len; i + =2, J + +) { One                     CharCH =Chs[i]; A                     CharCL = Chs[i +1]; -  -                     intIH =0, IL =0, V =0; the                     if(Ch >='A'&& CH <='F') { -IH =Ten+ (CH-'A'); -}Else if(Ch >='a'&& CH <='F') { -IH =Ten+ (CH-'a'); +}Else if(Ch >='0'&& CH <='9') { -IH = CH-'0'; +                     } A  at                     if(CL >='A'&& CL <='F') { -Il =Ten+ (CL-'A'); -}Else if(CL >='a'&& CL <='F') { -Il =Ten+ (CL-'a'); -}Else if(CL >='0'&& CL <='9') { -Il = CL-'0'; in                     } -  tov = (IH &0x0f) <<4) | (Il &0x0f); +                     //Assign Value -RET[J] = (byte) v; the                 } *             } $         }Panax Notoginseng         returnret; -}

On hex Coding algorithm

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.