这是我的代码,我想通过SMPP发送消息,但作为输出?即将到来:
public class Encoding
{
public static void main(String[] args) throws SocketTimeoutException, AlreadyBoundException, VersionException, SMPPProtocolException, UnsupportedOperationException, IOException
{
SubmitSM sm=new SubmitSM();
String strMessage="Pour se désinscrire du service TT ZONE, envoyez GRATUITEMENT « DTTZ » ";
String utf8 = new String(strMessage.getBytes("UTF-8"));
UCS2Encoding uc = UCS2Encoding.getInstance(true);
sm.setDataCoding(2);
sm.setMessageText(utf8);
System.out.println(sm.getMessageText());
}
}发布于 2014-03-21 12:14:52
你的问题是:
String strMessage="Pour se désinscrire du service TT ZONE, envoyez GRATUITEMENT « DTTZ » ";
String utf8 = new String(strMessage.getBytes("UTF-8"));你为什么要这么做?因为UCS2Encoding类接受一个String作为参数,它将处理编码本身。
只要做:
sm.setMessageText(strMessage);正如我在你提出的另一个问题中提到的,你混淆了许多概念。请注意,String是char的序列;它是,与使用的编码无关。Java内部使用UTF-16的事实在这里完全无关紧要。它可以使用UTF-32或EBCDIC,甚至可以使用信鸽,程序本身不会改变:
encode decode
String (char[]) --------> byte[] --------> String (char[])通过使用以字节数组作为参数的String构造函数,可以使用默认的JVM编码从这些字节创建char__s的seqeunce。它可能是UTF-8,也可能不是。
特别是,如果您正在使用Windows,默认编码将是windows-1252。让我们将上面的encode和decode替换为字符集名称。你所做的是:
UTF-8 windows-1252
String (char[]) -------> byte[] --------------> String (char[])“休斯顿,我们有麻烦了!”
有关更多细节,请参见Charset、CharsetEncoder和CharsetDecoder的javadocs。
https://stackoverflow.com/questions/22558366
复制相似问题