javasmppjsmpp

Encode french character in Java over smpp?


This is my code i am tring to Send Message Over SMPP but as Output ? is coming:

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());
}
}

Solution

  • Your problem is here:

    String strMessage="Pour se désinscrire du service TT ZONE, envoyez GRATUITEMENT « DTTZ » ";
    String utf8 = new String(strMessage.getBytes("UTF-8"));
    

    Why do you do that at all? Since the UCS2Encoding class accepts a String as an argument, it will take care of the encoding itself.

    Just do:

    sm.setMessageText(strMessage);
    

    As I mentioned in the other question you asked, you are mixing a LOT of concepts. Remind that a String is a sequence of chars; it is independent of the encoding you use. The fact that internally Java uses UTF-16 is totally irrelevant here. It could use UTF-32 or EBCDIC, or even use carrier pigeons, the process itself would not change:

                     encode           decode
    String (char[]) --------> byte[] --------> String (char[])
    

    And by using the String constructor taking a byte array as an argument, you create a seqeunce of chars from these bytes using the default JVM encoding. Which may, or may not, be UTF-8.

    In particular, if you are using Windows, the default encoding will be windows-1252. Let us replace encode and decode above with the charset names. What you do is:

                     UTF-8           windows-1252
    String (char[]) -------> byte[] --------------> String (char[])
    

    "Houston, we have a problem!"

    For more details, see the javadocs for Charset, CharsetEncoder and CharsetDecoder.