`

Java 中文于unicode 互转

阅读更多

关键字: java unicode convert 转换

Java代码 复制代码
  1.   

Java代码 复制代码
  1. package com.test;   
  2.   
  3. import java.io.BufferedReader;   
  4. import java.io.IOException;   
  5. import java.io.InputStreamReader;   
  6. import java.io.UnsupportedEncodingException;   
  7.   
  8. public class UnicodeByteUtil {   
  9.     public static void main(String[] args) {   
  10.         UnicodeByteUtil instance = new UnicodeByteUtil();   
  11.         BufferedReader reader = new BufferedReader(new InputStreamReader(   
  12.                 System.in));   
  13.         String line;   
  14.   
  15.         try {   
  16.             while ((line = reader.readLine()) != null) {   
  17.                 if (line.trim().equals("q"))   
  18.                     System.exit(0);   
  19.                 String s = instance.getBytes(line);   
  20.                 System.out.println("bytes:" + s);   
  21.                 // System.out.println("line:"+);   
  22.             }   
  23.         } catch (IOException e) {   
  24.             e.printStackTrace();   
  25.         }   
  26.     }   
  27.   
  28.     String getBytes(String s) {   
  29.         try {   
  30.             StringBuffer out = new StringBuffer("");   
  31.             byte[] bytes = s.getBytes("unicode");   
  32.             for (int i = 0; i < bytes.length; i++)   
  33.                 System.out.println(bytes[i]);   
  34.             for (int i = 2; i < bytes.length - 1; i += 2) { // *   
  35.                 out.append("\\u");   
  36.                 String str = Integer.toHexString(bytes[i + 1] & 0xff);// **   
  37.                 for (int j = str.length(); j < 2; j++) {   
  38.                     out.append("0");// ***   
  39.                 }   
  40.                 String str1 = Integer.toHexString(bytes[i] & 0xff);   
  41.                 out.append(str);   
  42.                 out.append(str1);   
  43.             }   
  44.             return out.toString();   
  45.         } catch (UnsupportedEncodingException e) {   
  46.             e.printStackTrace();   
  47.             return null;   
  48.         }   
  49.     }   
  50. }  
package com.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class UnicodeByteUtil {
	public static void main(String[] args) {
		UnicodeByteUtil instance = new UnicodeByteUtil();
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				System.in));
		String line;

		try {
			while ((line = reader.readLine()) != null) {
				if (line.trim().equals("q"))
					System.exit(0);
				String s = instance.getBytes(line);
				System.out.println("bytes:" + s);
				// System.out.println("line:"+);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	String getBytes(String s) {
		try {
			StringBuffer out = new StringBuffer("");
			byte[] bytes = s.getBytes("unicode");
			for (int i = 0; i < bytes.length; i++)
				System.out.println(bytes[i]);
			for (int i = 2; i < bytes.length - 1; i += 2) { // *
				out.append("\\u");
				String str = Integer.toHexString(bytes[i + 1] & 0xff);// **
				for (int j = str.length(); j < 2; j++) {
					out.append("0");// ***
				}
				String str1 = Integer.toHexString(bytes[i] & 0xff);
				out.append(str);
				out.append(str1);
			}
			return out.toString();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
}


Java代码 复制代码
  1. public   static   String   loadConvert(String   theString)   {      
  2.                   char   aChar;      
  3.                   int   len   =   theString.length();      
  4.                   StringBuffer   outBuffer   =   new   StringBuffer(len);      
  5.        
  6.                   for   (int   x=0;   x<len;   )   {      
  7.                           aChar   =   theString.charAt(x++);      
  8.                           if   (aChar   ==   '\\')   {      
  9.                                   aChar   =   theString.charAt(x++);      
  10.                                   if   (aChar   ==   'u')   {      
  11.                                           //   Read   the   xxxx      
  12.                                           int   value=0;      
  13.           for   (int   i=0;   i<4;   i++)   {      
  14.                   aChar   =   theString.charAt(x++);      
  15.                   switch   (aChar)   {      
  16.                       case   '0':   case   '1':   case   '2':   case   '3':   case   '4':      
  17.                       case   '5':   case   '6':   case   '7':   case   '8':   case   '9':      
  18.                             value   =   (value   <<   4)   +   aChar   -   '0';      
  19.             break;      
  20.       case   'a':   case   'b':   case   'c':      
  21.                                                       case   'd':   case   'e':   case   'f':      
  22.             value   =   (value   <<   4)   +   10   +   aChar   -   'a';      
  23.             break;      
  24.       case   'A':   case   'B':   case   'C':      
  25.                                                       case   'D':   case   'E':   case   'F':      
  26.             value   =   (value   <<   4)   +   10   +   aChar   -   'A';      
  27.             break;      
  28.       default:      
  29.                                                               throw   new   IllegalArgumentException(      
  30.                                                                                         "Malformed   \\uxxxx   encoding.");      
  31.                                                   }      
  32.                                           }      
  33.                                           outBuffer.append((char)value);      
  34.                                   }   else   {      
  35.                                           if   (aChar   ==   't')   aChar   =   '\t';      
  36.                                           else   if   (aChar   ==   'r')   aChar   =   '\r';      
  37.                                           else   if   (aChar   ==   'n')   aChar   =   '\n';      
  38.                                           else   if   (aChar   ==   'f')   aChar   =   '\f';      
  39.                                           outBuffer.append(aChar);      
  40.                                   }      
  41.                           }   else      
  42.                                   outBuffer.append(aChar);      
  43.                   }      
  44.                   return   outBuffer.toString();      
  45.           }  
public   static   String   loadConvert(String   theString)   {   
                  char   aChar;   
                  int   len   =   theString.length();   
                  StringBuffer   outBuffer   =   new   StringBuffer(len);   
    
                  for   (int   x=0;   x<len;   )   {   
                          aChar   =   theString.charAt(x++);   
                          if   (aChar   ==   '\\')   {   
                                  aChar   =   theString.charAt(x++);   
                                  if   (aChar   ==   'u')   {   
                                          //   Read   the   xxxx   
                                          int   value=0;   
          for   (int   i=0;   i<4;   i++)   {   
                  aChar   =   theString.charAt(x++);   
                  switch   (aChar)   {   
                      case   '0':   case   '1':   case   '2':   case   '3':   case   '4':   
                      case   '5':   case   '6':   case   '7':   case   '8':   case   '9':   
                            value   =   (value   <<   4)   +   aChar   -   '0';   
            break;   
      case   'a':   case   'b':   case   'c':   
                                                      case   'd':   case   'e':   case   'f':   
            value   =   (value   <<   4)   +   10   +   aChar   -   'a';   
            break;   
      case   'A':   case   'B':   case   'C':   
                                                      case   'D':   case   'E':   case   'F':   
            value   =   (value   <<   4)   +   10   +   aChar   -   'A';   
            break;   
      default:   
                                                              throw   new   IllegalArgumentException(   
                                                                                        "Malformed   \\uxxxx   encoding.");   
                                                  }   
                                          }   
                                          outBuffer.append((char)value);   
                                  }   else   {   
                                          if   (aChar   ==   't')   aChar   =   '\t';   
                                          else   if   (aChar   ==   'r')   aChar   =   '\r';   
                                          else   if   (aChar   ==   'n')   aChar   =   '\n';   
                                          else   if   (aChar   ==   'f')   aChar   =   '\f';   
                                          outBuffer.append(aChar);   
                                  }   
                          }   else   
                                  outBuffer.append(aChar);   
                  }   
                  return   outBuffer.toString();   
          }

Java代码 复制代码
  1. private   static   String   unicodeToGB(String   s)   {      
  2.           StringBuffer   sb   =   new   StringBuffer();      
  3.           StringTokenizer   st   =   new   StringTokenizer(s,   "\\u");      
  4.           while   (st.hasMoreTokens())   {      
  5.               sb.append(   (char)   Integer.parseInt(st.nextToken(),   16));      
  6.           }      
  7.           return   sb.toString();      
  8.       }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics