接收手机中文短信,存进数据库是乱码,如何转换,高分求解!!!

yunmengxiaofei 2003-07-23 11:36:55
接收手机发过来的中文短信,存进数据库全是乱码,怎么样转换啊,高手救我!!高分!高分!
...全文
169 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
pyyukiki 2003-07-27
  • 打赏
  • 举报
回复
如何将手机短信内容直接存到电脑的数据库上啊.
ludf 2003-07-27
  • 打赏
  • 举报
回复
有关手机中文问题传输的解决办法
服务器到客户端:
----------------------------------------------------------------------
下面代码是服务器端把字符写到Client端,经过gbEncoding()方法,所有的字符编码成:\uXXXX.
----------------------------------------------------------------------
代码:--------------------------------------------------------------------------------
/**
* Write the String data
*
* @param out
* @param value
*/
public static void writeUnicode(final DataOutputStream out, final String value) throws ActionException {
try {
final String unicode = StringFormatter.gbEncoding( value );
final byte[] data = unicode.getBytes();
final int dataLength = data.length;

System.out.println( "Data Length is: " + dataLength );
System.out.println( "Data is: " + value );
out.writeInt( dataLength );
out.write( data, 0, dataLength );
} catch (IOException e) {
throw new ActionException( IMDefaultAction.class.getName(), e.getMessage() );
}
}
--------------------------------------------------------------------------------

----------------------------------------------------------------------
以下代码是gbEncoding()方法,把双字节字符转换成\uXXXX,ASIIC码在前面补00。
----------------------------------------------------------------------
/**
* This method will encode the String to unicode.
*
* @param gbString
* @return
*/

代码:--------------------------------------------------------------------------------
public static String gbEncoding( final String gbString ) {
char[] utfBytes = gbString.toCharArray();
String unicodeBytes = "";
for( int byteIndex = 0; byteIndex < utfBytes.length; byteIndex ++ ) {
String hexB = Integer.toHexString( utfBytes[ byteIndex ] );
if( hexB.length() <= 2 ) {
hexB = "00" + hexB;
}
unicodeBytes = unicodeBytes + "\\u" + hexB;
}
System.out.println( "unicodeBytes is: " + unicodeBytes );
return unicodeBytes;
}
--------------------------------------------------------------------------------

----------------------------------------------------------------------
在客户端收到服务器的数据,先将其一个一个字符解码。双字节显示正常。
----------------------------------------------------------------------

代码:--------------------------------------------------------------------------------
/**
* This method will decode the String to a recognized String
* in ui.
* @param dataStr
* @return
*/
private StringBuffer decodeUnicode( final String dataStr ) {
int start = 0;
int end = 0;
final StringBuffer buffer = new StringBuffer();
while( start > -1 ) {
end = dataStr.indexOf( "\\u", start + 2 );
String charStr = "";
if( end == -1 ) {
charStr = dataStr.substring( start + 2, dataStr.length() );
} else {
charStr = dataStr.substring( start + 2, end);
}
char letter = (char) Integer.parseInt( charStr, 16 ); // 16进制parse整形字符串。
buffer.append( new Character( letter ).toString() );
start = end;
}
return buffer;
}
--------------------------------------------------------------------------------

----------------------------------------------------------------------
客户端到服务器:
----------------------------------------------------------------------
客户端使用下面方法把手机端的字符编码成ISO-8859-1,传给服务器。
----------------------------------------------------------------------

代码:--------------------------------------------------------------------------------
/**
* write the String data
* @param value
* @param outData
*/
private void writeSjis(DataOutputStream outData, String value) {
try {
byte[] data = null;
// data = ( value ).getBytes( "UTF-8" );
data = ( value ).getBytes( "ISO8859_1" );
outData.writeInt(data.length);
outData.write(data, 0, data.length);

System.out.println(" data.length: " + data.length);
System.out.println(" data.value: " + value);
} catch (Exception ex) {
System.out.println(" write error ");
ex.printStackTrace();
}
}
--------------------------------------------------------------------------------

----------------------------------------------------------------------
服务器端收到客户端字符流,是用下面方法将其转为UTF-8,以后的操作都是基于UTF-8编码。SQLServer可能会由于内吗不通有不同的变换,所以存取数据库是还要是具体的DB内码作相应的处理。
----------------------------------------------------------------------

代码:--------------------------------------------------------------------------------
/**
*
* @param iso
* @return
*/
public static String isoToUtf( final String iso ) {
String utfString = iso;
if( iso != null ) {
try {
utfString = new String( iso.getBytes( "ISO-8859-1" ), "UTF-8" );
} catch ( UnsupportedEncodingException e ) {
utfString = iso;
}
} else {
utfString = "";
}
return utfString;
}

ludf 2003-07-27
  • 打赏
  • 举报
回复
这段代码仅供参考:
//transfer code from GBK to iso-8859-1 (向数据库插入)
public String transferCode(String a) {
String b = "";
try {
b = new String(a.getBytes(),"iso-8859-1");
}
catch (java.io.UnsupportedEncodingException e) {
System.out.println("java.io.UnsupportedEncodingException !!!");
e.printStackTrace();
}
return b;
}

//transfer code from iso-8859-1 to GBK (从数据库读出)
public String transferCode8859toGBK (String a) {
String b = "";
try {
b = new String(a.getBytes("iso-8859-1"));
}
catch (java.io.UnsupportedEncodingException e) {
System.out.println("java.io.UnsupportedEncodingException !!!");
e.printStackTrace();
}
return b;
}
phsea 2003-07-27
  • 打赏
  • 举报
回复
不是吧,你不能这么打印byte值的,特别如果你是中文,

我希望你用utf试试,接受显示的时候用字符串显示看看

对了,能告诉我如何做短信开发么?

希望多多交流

flyjava@163.com
yunmengxiaofei 2003-07-23
  • 打赏
  • 举报
回复
兄弟们,帮我想想啊
yunmengxiaofei 2003-07-23
  • 打赏
  • 举报
回复
Msgcontent = new String(Msgcontent.trim().getBytes("GB2312"),"iso-8859-1");
Msgcontent = new String(Msgcontent.trim().getBytes("UTF-8"),"GB2312");

这样都不行,怎么办??
yunmengxiaofei 2003-07-23
  • 打赏
  • 举报
回复
56FD5BB691CC97625BF94E8E662F

这是我打印出来的byte值,存进Oracle就是乱码,用 peterli007(李冰) 的方法转换之后老是return null,怎么办??
yunmengxiaofei 2003-07-23
  • 打赏
  • 举报
回复
我试试看
peterli007 2003-07-23
  • 打赏
  • 举报
回复
public static String read_utf8( byte[] data ) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream(2 + data.length);
DataOutputStream dos = new DataOutputStream(bos);
dos.writeShort(data.length);
dos.write(data, 0, data.length);
byte[] jdata = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(jdata, 0,
jdata.length);
DataInputStream dis = new DataInputStream(bis);
return dis.readUTF();
}
catch (IOException e) {
return null;
}
}
phsea 2003-07-23
  • 打赏
  • 举报
回复
你转换称utf8格式试试看

再建议:
接受的时候应该用byte值,存入rms

我也不知道了,

呵呵,good luck
yunmengxiaofei 2003-07-23
  • 打赏
  • 举报
回复
兄弟们,帮我想想啊

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧