16 binary and Byte conversions

Source: Internet
Author: User

This article turns from: http://franksinger.iteye.com/blog/614540

In Java, byte is used in binary notation to occupy 8 bits, and we know that each character of the 16 binary needs to be represented by 4-bit bits (23 + 22 + 21 + 20 = 15), so we can convert each byte to two corresponding 16 characters. That is, the high 4 bits of byte and the lower 4 bits are converted to the corresponding 16-character H and L and combined to get the result of the byte conversion to the 16 string, new String (H) + new String (L). That is, byte in hexadecimal notation only occupies 2 digits.

In the same way, the opposite conversion converts two 16 characters into a byte, as in principle.

Based on the above principles, we can convert the byte[] array to a 16 binary string, and of course we can convert the 16 string into a byte[] array.

/**

   * convert byte[] to hex string. Here we can convert byte to int and then use integer.tohexstring ( int) to convert to a 16 binary string.     *  @param  src byte[] data     *  @return  hex  String     */       public static string bytestohexstring ( BYTE[]&NBSP;SRC) {       StringBuilder stringBuilder = new  StringBuilder ("");        if  (src == null | |  src.length <= 0)  {           return  null;       &nbsp}        for  int i = 0;  i < src.length; i++)  {            int v = src[i] & 0xFF;    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;  string hv = integer.tohexstring (v);            if  (Hv.length ()  < 2)  {                stringbuilder.append (0);            }             stringbuilder.append (HV);       &nbsp        return stringbuilder.tostring ();   }   /**    * Convert hex string to byte[]     *  @param  hexString the hex string    *  @return   byte[]    */   public static byte[] hexstringtobytes (string  hexstring)  {       if  (hexstring == null | |  hexstring.equals ("")) {           return null;       &NBSP;&NBSP}        hexstring = hexstring.touppercase ();        int length = hexstring.length ()  / 2;        char[] hexchars = hexstring.tochararray ();        byte[] d = new byte[length];        for  (int i = 0; i < length; i++ )  {           int pos = i * 2;            d[i] =  (Byte)   (Chartobyte hexchars[ POS])  << 4 | chartobyte (hexchars[pos + 1]);        }        return d;   }   /**    * Convert char to byte    * @ param c char    *  @return  byte    */

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.