16 Conversion of binary and string (including Chinese)

Source: Internet
Author: User

Package XXXX;

Import Java.io.ByteArrayOutputStream;

/**
* Description string (contains Chinese) and conversion between 16 binary
*/
public class Codechange {
/**
* 16 binary numeric Character set
*/
private static String hexstring= "0123456789ABCDEF";

 
 
 public static void Main (string[] args) {
   system.out.println (Tostringhex ("41"));
 }
 
 /**
  * Conversion string hexadecimal encoding
  */
 public static string tohexstring (string s) {   
  string str= "";   
  for (int i=0;i<s.length (); i++) {   
   int ch = (int) s.charat (i);   
   string s4 = Integer.tohexstring (ch);   
   str = str + s4;
  }  
  return str;  
 }

 /**
  * Convert hexadecimal encoding to string
  */
 public static string Tostringhex (String s) {
  byte[] Bakeyword = new Byte[s.length ()/2];
  for (int i = 0; i < bakeyword.length; i++) {
   try{
     Bakeyword[i] = (byte) (0xFF & Integer.parseint (s.substring (i*2, i*2+2), 16));
   }catch (Exception e) {
    e.printstacktrace ();
   }
  }

try{
s = new String (Bakeyword, "Utf-8");//utf-16le:not
}catch (Exception E1) {
E1.printstacktrace ();
}
return s;
}

/**
* Encode a string into 16 digits for all characters (including Chinese)
*/
public static string encode (String str) {
Get byte array by default encoding
Byte[] Bytes=str.getbytes ();
StringBuilder sb=new StringBuilder (bytes.length*2);
Splits each byte in a byte array into a 2-bit 16-decimal integer
for (int i=0;i<bytes.length;i++) {
Sb.append (Hexstring.charat ((bytes[i]&0xf0) >>4));
Sb.append (Hexstring.charat ((bytes[i]&0x0f) >>0));
}
return sb.tostring ();
}
/**
* Decodes 16 binary digits into a string for all characters (including Chinese)
*/
public static string decode (string bytes)
{
Bytearrayoutputstream baos=new Bytearrayoutputstream (Bytes.length ()/2);
Assemble every 2 bits of 16 integers into one byte
for (int i=0;i<bytes.length (); i+=2)
Baos.write ((Hexstring.indexof (Bytes.charat (i)) <<4 |hexstring.indexof (Bytes.charat (i+1)));
return new String (Baos.tobytearray ());
}
}

16 Conversion of binary and string (including Chinese)

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.