On the conversion between binary, Decimal, 16 and string _java

Source: Internet
Author: User
Tags array to string

1. Byte ext. 10 system

Direct use of (int) type conversions.


   * * bytes 10
  /public static int Byte2int (byte b) {
    int r = (int) b;
    return r;
  }

2.10 Forward bytes

Direct use of (byte) type conversions.


   * * 10 forward bytes/public
  static byte int2byte (int i) {
    byte R = (byte) i;
    return r;
  }

3. Byte array ext. 16 binary string

For each byte, first and 0xFF to do with the operation, and then use the integer.tohexstring () function, if the result is only 1 digits, you need to add 0 in front.


   * * * byte array to 16 feed string
   *
  /public static string bytes2hexstring (byte[] b) {
    string r = ' "
    
    ; for (int i = 0; i < b.length i++) {
      String hex = integer.tohexstring (B[i] & 0xFF);
      if (hex.length () = = 1) {
        hex = ' 0 ' + hex;
      }
      R + + hex.touppercase ();
    }
    
    return r;
  }

4.16 binary string byte array

This is more complex, each 16 character is 4bit, a byte is 8bit, so two 16 characters are converted to 1 bytes, the 1th character is converted to Byte, then left 4 digits, then byte or operation with the 2nd character, which converts two characters to 1 bytes.

/
   * character conversion to byte
   /
  private static byte Chartobyte (char c) {return
    (byte) "0123456789ABCDEF". IndexOf (c); c5/>}
   * * 16 feed string to byte array
   /public
  static byte[] hexstring2bytes (String hex) {
      
      if (hex = null) | | (Hex.equals ("")) {return
        null;
      }
      else if (hex.length ()%2!= 0) {return
        null;
      }
      else{        
        hex = hex.touppercase ();
        int len = Hex.length ()/2;
        Byte[] B = new Byte[len];
        char[] HC = Hex.tochararray ();
        for (int i=0; i<len; i++) {
          int p=2*i;
          B[i] = (byte) (Chartobyte (Hc[p]) << 4 | chartobyte (hc[p+1));
       return b;
      }      
      
  }

5. Byte array spin string

Use the new String () directly.

/
   * * byte array spin string *
  /public static string bytes2string (byte[] b) throws Exception {
    string r = new String (b, " UTF-8 ");    
    return r;
  }

6. string byte array

Direct use of GetBytes ().


   * * * string byte array/public
  static byte[] String2bytes (string s) {
    byte[] r = s.getbytes ();
    return r;
  }

7. 16 String string

Convert to byte[] First, then convert to string.


   * * 16 Feed string/spin string
   *
  /public static string hex2string (String hex) throws exception{
    string r = Bytes2string (Hexstring2bytes (hex));    
    return r;
  }

8. string ext. 16 binary string

Convert to byte[] First, then convert to 16-in string.


   * * String 16 into String *
   /public
  static string string2hexstring (String s) throws exception{string
    r = Bytes2hexstring (String2bytes (s));    
    return r;
  }

Main function:

public static void Main (string[] args) throws exception{    
    byte B1 = (byte);
    System.out.println ("1. Byte to 10": "+ Byte2int (B1));
    
    int i =;
    System.out.println ("2.10 bytes:" + int2byte (i));
    
    byte[] B2 = new byte[]{(byte) 0xFF, (Byte) 0x5f, (Byte) 0x6, (byte) 0x5a};
    System.out.println ("3. Byte array to 16 binary string:" + bytes2hexstring (B2));
    
    string S1 = new String ("1da47c");
    System.out.println ("4.16 binary string-byte array:" + arrays.tostring (hexstring2bytes (S1));
    
    System.out.println ("5. Byte array spin string:" + bytes2string (B2));
    
    System.out.println ("6. String to byte array:" + arrays.tostring (string2bytes (S1)));
    
    System.out.println ("7.16 binary string:" + hex2string (S1));
    
    String s2 = new String ("hello!");
    System.out.println ("8. String to 16 binary string:" + string2hexstring (S2));
  }

Run Result:

1. Byte to 10 binary: 2.10 bytes
:
3. Byte array ext. 16 binary string: ff5f065a
4.16-byte array: [MB, -92, 124]
5. Byte array to string:? _z 6. String-byte array: [I, A,,,,,
]
7.16 string to string:? |
8. String ext. 16 Binary string: 48656C6C6F21

The above article on the binary, decimal, 16, the conversion between strings is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.