问题一
public static String GB2312_Unicode( String gb2312 )
{
String unicode = "";
try
{
    byte[] test = gb2312.getBytes("Unicode");
    String tag = "\\u";
    for(int i=2;i<test.length;i+=2)
    {
unicode = unicode + tag + Integer.toHexString(test[i+1])+Integer.toHexString(test[i]);
    }
    unicode = unicode.replace( "ffffff","" );
}
catch ( java.io.IOException e )
{
    logger.error( e );
}
return unicode;
}上面为获得汉字的Unicode码,问题出在 Integer.toHexString(test[i+1])+Integer.toHexString(test[i]) 处,为什么有些汉字转码出来的为 **** 可以直接使用,而有些转码出来的则为 ********** ,其中多了6个f?
问题二
public static String Unicode_GB2312( String unicode )
{
String gb2312 = "";try
{
    gb2312 = new String(unicode.getBytes("GB2312"));
}
catch ( java.io.IOException e )
{
    logger.error( e );
}
return gb2312;
}上面为Unicode码转GB2312,参数unicode为我从页面获得的 \u4e2d\u534e\u4eba\u6c11\u5171\u548c\u56fd unicode码字符串,但是返回的为什么还是原来的unicode码字符串?而我在里面如果写成常量 String unicode = "\u4e2d\u534e\u4eba\u6c11\u5171\u548c\u56fd"; 则可以正常的返回我GB2312汉字。

解决方案 »

  1.   

    先解决你第二个问题,String unicode = "\u4e2d\u534e\u4eba\u6c11\u5171\u548c\u56fd"这个字符串中"\"是转意符,这个就知道为什么会有不同了。
    你这个转编码的算法肯定是不能正常工作的。
    第一个问题没有编译,不知道具体问题出在哪里,但是肯定这个算法不理想。因为这个转为16进制后位数肯定是有问题的
    贴一段代码给你。但是反便宜的算法我还没有找到
    private String native2asciiString(String data) {
    StringBuffer b = new StringBuffer(); // JavaScript is sensitive to some UNICODE characters. So for
    // the sake of simplicity, we just escape everything.
    // notice that "\" is so special,so do some change at the some time
    char[] ch = data.toCharArray();
    for (int j = 0; j < ch.length; j++) {
    if (ch[j] < 128) {
    String entity = entities[ch[j]];
    if (entity == null)
    b.append(ch[j]);
    else
    b.append(entity);
    } else {
    b.append(native2ascii(ch[j]));
    }
    } return b.toString(); } /**
     * Create an escaped Unicode character
     * 
     * @param c
     * @return The unicode character in escaped string form
     */
    private static String native2ascii(char c) {
    StringBuffer b = new StringBuffer();
    for (int i = 0; i < 4; i++) {
    b.append(Integer.toHexString(c & 0x000F).toUpperCase());
    c >>>= 4;
    }
    b.append("u\\");
    return b.reverse().toString();
    }
      

  2.   

    谢谢那我在前台页面该怎样输入后台才能接收到 \u4e2d\u534e\u4eba\u6c11\u5171\u548c\u56fd 呢?我在后台打印出来的已经是 \u4e2d\u534e\u4eba\u6c11\u5171\u548c\u56fd另外有没有标准的 GB2312_Unicode 方法呢?我这个方法写的确实有问题
      

  3.   

    输入是没有什么问题的,直接输入就可以了,只要不是通过程序直接写死。
    如果你想给一个默认值的话就写成 String s = "\\u4e2d"。
    GB2312_Unicode的标准方法我是没有看到过有,如果你非要用编码的方式解决汉化问题可以考虑使用别的编码方式,apache的开源项目commons里面有关于编码的实用包。
      

  4.   

    再帖段解编码的代码
    private String loadConvert (char[] in, int off, int len, char[] convtBuf) {
            if (convtBuf.length < len) {
                int newLen = len * 2;
                if (newLen < 0) {
            newLen = Integer.MAX_VALUE;
        } 
        convtBuf = new char[newLen];
            }
            char aChar;
            char[] out = convtBuf; 
            int outLen = 0;
            int end = off + len;        while (off < end) {
                aChar = in[off++];
                if (aChar == '\\') {
                    aChar = in[off++];   
                    if(aChar == 'u') {
                        // Read the xxxx
                        int value=0;
        for (int i=0; i<4; i++) {
            aChar = in[off++];  
            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.");
                            }
                         }
                        out[outLen++] = (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'; 
                        out[outLen++] = aChar;
                    }
                } else {
            out[outLen++] = (char)aChar;
                }
            }
            return new String (out, 0, outLen);
        }
      

  5.   

    另外一段编码代码
    private String saveConvert(String theString, boolean escapeSpace) {
            int len = theString.length();
            int bufLen = len * 2;
            if (bufLen < 0) {
                bufLen = Integer.MAX_VALUE;
            }
            StringBuffer outBuffer = new StringBuffer(bufLen);        for(int x=0; x<len; x++) {
                char aChar = theString.charAt(x);
                // Handle common case first, selecting largest block that
                // avoids the specials below
                if ((aChar > 61) && (aChar < 127)) {
                    if (aChar == '\\') {
                        outBuffer.append('\\'); outBuffer.append('\\');
                        continue;
                    }
                    outBuffer.append(aChar);
                    continue;
                }
                switch(aChar) {
    case ' ':
        if (x == 0 || escapeSpace) 
    outBuffer.append('\\');
        outBuffer.append(' ');
        break;
                    case '\t':outBuffer.append('\\'); outBuffer.append('t');
                              break;
                    case '\n':outBuffer.append('\\'); outBuffer.append('n');
                              break;
                    case '\r':outBuffer.append('\\'); outBuffer.append('r');
                              break;
                    case '\f':outBuffer.append('\\'); outBuffer.append('f');
                              break;
                    case '=': // Fall through
                    case ':': // Fall through
                    case '#': // Fall through
                    case '!':
                        outBuffer.append('\\'); outBuffer.append(aChar);
                        break;
                    default:
                        if ((aChar < 0x0020) || (aChar > 0x007e)) {
                            outBuffer.append('\\');
                            outBuffer.append('u');
                            outBuffer.append(toHex((aChar >> 12) & 0xF));
                            outBuffer.append(toHex((aChar >>  8) & 0xF));
                            outBuffer.append(toHex((aChar >>  4) & 0xF));
                            outBuffer.append(toHex( aChar        & 0xF));
                        } else {
                            outBuffer.append(aChar);
                        }
                }
            }
            return outBuffer.toString();
        }
      

  6.   

    谢谢,问题一我搞定了,用的是jdk自带的native2ascii问题二我还是不清楚原因,我在页面无论输入\\u4e2d返回的是\u4e2d,输入\\\u4e2d返回的是\\u4e2d???