string and hexadecimal numbers go with each other

Source: Internet
Author: User

Don't say much nonsense, please see:

    /** * String to hexadecimal String * @param string str ASCII string to be converted * @return string separated by a space between each byte, such as: [6C 6B] */public static string Str2hexstr (String str) {char[] chars = "0123456789ABCDEF". ToChar    
        Array ();  
        StringBuilder sb = new StringBuilder ("");    
        byte[] bs = Str.getbytes ();    
          
        int bit;    
            for (int i = 0; i < bs.length i++) {bit = (Bs[i] & 0x0f0) >> 4;    
            Sb.append (Chars[bit]);    
            bit = Bs[i] & 0x0f;  
            Sb.append (Chars[bit]);  
        Sb.append (");    
    Return sb.tostring (). Trim (); /** * 16 Binary conversion String * @param string str byte string (no separator between byte such as: [616c6b]) * @return string corresponding word    
        string/public static string Hexstr2str (String hexstr) {string str = "0123456789ABCDEF";    
   char[] Hexs = Hexstr.tochararray ();     byte[] bytes = new Byte[hexstr.length ()/2];    
  
        int n;    
            for (int i = 0; i < bytes.length i++) {n = str.indexof (hexs[2 * i)) * 16;    
            n + = str.indexof (hexs[2 * i + 1]);    
        Bytes[i] = (byte) (n & 0xff);    
    Return to New String (bytes);  
    /** * bytes converted to hexadecimal string * @param byte[] b byte array * @return String separated by a space between each byte *  
        public static string Byte2hexstr (byte[] b) {string stmp= "";  
        StringBuilder sb = new StringBuilder ("");  
            for (int n=0;n<b.length;n++) {stmp = Integer.tohexstring (b[n) & 0xFF); Sb.append ((Stmp.length () ==1)?  
            "0" +stmp:stmp);  
        Sb.append ("");  
    Return sb.tostring (). toUpperCase (). Trim (); 
  /** * bytes string converted to byte * @param string src byte string with no separator between each byte * @return byte[]   */public static byte[] Hexstr2bytes (String src) {int m=0,n=0;  
        int l=src.length ()/2;  
        System.out.println (l);  
        byte[] ret = new Byte[l];  
            for (int i = 0; i < L; i++) {m=i*2+1;  
            n=m+1;  
        Ret[i] = Byte.decode ("0x" + src.substring (i*2, m) + src.substring (m,n));  
    return ret; The string of/** * string is converted to a Unicode string * @param string StrText full-width strings * @return string each Unicode without  
    Separator * @throws Exception */public static string Strtounicode (String strText) throws Exception  
        {char C;  
        StringBuilder str = new StringBuilder ();  
        int INTASC;  
        String Strhex;  
            for (int i = 0; i < strtext.length (); i++) {c = Strtext.charat (i);  
            INTASC = (int) c;  
            Strhex = integer.tohexstring (INTASC); if (Intasc > 128) Str.append ("\\u" + strhex);  
        else//low in front complement Str.append ("\\u00" + strhex);  
    return str.tostring (); A string converted to string/** * Unicode @param string hex 16 string (one Unicode 2byte) * @retur   
        n String full-angle String */public static string unicodetostring (string hex) {int t = hex.length ()/6;  
        StringBuilder str = new StringBuilder ();  
            for (int i = 0; i < T; i++) {String s = hex.substring (i * 6, (i + 1) * 6);  
            The high position needs to be filled with 00 and then the String S1 = s.substring (2, 4) + "00";  
            Low-level direct turn String s2 = s.substring (4);  
            Converts a 16 string to int int n = integer.valueof (S1) + integer.valueof (S2, 16);  
            converts int to character char[] chars = Character.tochars (n);  
        Str.append (New String (chars)); Return to Str.ToString ();  }


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.