8的确是UCS2编码..
还是UCS2 TO GB编码的问题
我抄个答案吧:
汉字不同编码转换的问题 UCS2转码的心得
看到很多朋友都对汉字的unicode编码有问题, 还需要什么”unicode汉字对应表”...
一直想回贴子,但太忙(包括忙着从论坛中索取自己需要的东西).
其实汉字的编码转换很简单,我相信C++也应该有相应的函数.希望这些代码对用Java的朋友有帮助.
/*
* UnicodeTest.java
*
* Created on July 29, 2003, 12:59 PM
*/
/**
*
* @author abc
* @version
*/
public class UnicodeTest
{
public static void main(String args[])
{
UnicodeTest UT = new UnicodeTest();
UT.test1();
}
public void test1()
{
String str = "测试信息abc123";
try
{
byte[] b = str.getBytes("GBK");
System.out.println(str + " -(GBK)编码: " + bytesToHexStr(b));
System.out.println("");
str = new String(b, "GBK");
System.out.println("从GBK编码 " + bytesToHexStr(b) + " 重新转换为字串: " + str);
System.out.println("");
b = str.getBytes("UnicodeBigUnmarked");
System.out.println(str + " -(UCS2)编码: " + bytesToHexStr(b));
System.out.println("");
str = new String(b, "UnicodeBigUnmarked");
System.out.println("从(UCS2)编码 " + bytesToHexStr(b) + " 重新转换为字串: " + str);
System.out.println("");
b = str.getBytes("ASCII");
System.out.println(str + " -(ASCII)编码: " + bytesToHexStr(b));
System.out.println("");
}
catch(Exception e){}
}
private String bytesToHexStr(byte[] b)
{
if (b == null) return "";
StringBuffer strBuffer = new StringBuffer(b.length * 3);
for(int i = 0; i < b.length; i++)
{
strBuffer.append(Integer.toHexString(b & 0xff));
strBuffer.append(" ");
}
return strBuffer.toString();
}
}
运行此小程序的输出结果是:
测试信息abc123 -(GBK)编码: b2 e2 ca d4 d0 c5 cf a2 61 62 63 31 32 33
从GBK编码 b2 e2 ca d4 d0 c5 cf a2 61 62 63 31 32 33 重新转换为字串: 测试信息abc123
测试信息abc123 -(UCS2)编码: 6d 4b 8b d5 4f e1 60 6f 0 61 0 62 0 63 0 31 0 32 0 33
从(UCS2)编码 6d 4b 8b d5 4f e1 60 6f 0 61 0 62 0 63 0 31 0 32 0 33 重新转换为字串: 测试信息abc123
测试信息abc123 -(ASCII)编码: 3f 3f 3f 3f 61 62 63 31 32 33
这段时间都在做联通的SP网关程序,原来我是做web应用的,对数据库之类的java编程比较熟悉。原来也从来没有接触过短信网关方面的系统设计和编程。在这个过程中碰到了几个比较棘手的问题,UCS2的转码就是其中一个。
刚开始我们公司的业务没有涉及到中文信息,所以没有注意这个问题,用户只需要发送字母和数字就可以了,但是最近几天我在数据库中发现了一些乱码,Messagecoding=8,我猜测可能和用户手机的输入法有关系,即使是阿拉伯数字也有双字节的,比如“8”和“8”。
下面这段代码是底层的API:
.........(read bytes from input)
//获取消息编码
MessageCoding = bodybytes[44];
//获取短消息内容的长度
SGIP_Command.BytesCopy(bodybytes, abyte0, 45, 48, 0);
MessageLength = SGIP_Command.Bytes4ToInt(abyte0);
//创建一个内容长度的Byte
MessageByte = new byte[MessageLength];
//将Message copy 到 MessageByte 中
SGIP_Command.BytesCopy(bodybytes, MessageByte, 49, (49 + MessageLength) - 1,0);
//开始解码转换
if(MessageCoding==8){//如果编码格式为UCS2,就转换成普通的String
try {
MessageContent = new String(MessageByte,"UnicodeBigUnmarked");
} catch (UnsupportedEncodingException e) {
}
}else{
MessageContent = new String(MessageByte);
}
实际上在java中就只需要一句MessageContent = new String(MessageByte,"UnicodeBigUnmarked");就可以转换过来,再保存到数据库中就不会是乱码了。
进行转换后,我还用了另外一个函数把类似“8”这样的GBK编码的阿拉伯数字都转换成了ASCII的数字。这样对业务逻辑有帮助。
还有一点要说明一下,GB2312是一个比较早版本的中文编码格式,GBK是新的中文编码格式,GBK是GB2312的超集,GB2312是GBK的真子集。
我的底层API是使用的英斯克的底层api,不过我修改了英斯克的API几个不完善的地方。希望对碰到和我一样问题的同志有点帮助。
---
import java.io.UnsupportedEncodingException; public class Test2 { /** * @param args */ public static void main(String args[]) { Test2 UT = new Test2(); UT.test1(); } public void test1() { String str = "您好【ABC】"; try { byte[] b = str.getBytes("GBK"); System.out.println(str + " -(GBK)编码: " + bytesToHexStr(b)); System.out.println(""); str = new String(b, "GBK"); System.out.println("从GBK编码 " + bytesToHexStr(b) + " 重新转换为字串: " + str); System.out.println(""); b = str.getBytes("UnicodeBigUnmarked"); System.out.println(str + " -(UCS2)编码: " + bytesToHexStr(b)); System.out.println(""); str = new String(b, "UnicodeBigUnmarked"); System.out.println("从(UCS2)编码 " + bytesToHexStr(b) + " 重新转换为字串: " + str); System.out.println(""); b = str.getBytes("ASCII"); System.out.println(str + " -(ASCII)编码: " + bytesToHexStr(b)); System.out.println(""); } catch(Exception e){} } private String bytesToHexStr(byte[] b) { if (b == null) return ""; StringBuffer strBuffer = new StringBuffer(b.length * 3); for(int i = 0; i < b.length; i++) { strBuffer.append(Integer.toHexString(b[i] & 0xff)); strBuffer.append(" "); } return strBuffer.toString(); } }
此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/69698.html