pesaply / smslib

Automatically exported from code.google.com/p/smslib
0 stars 0 forks source link

Incorrect 8bit encoding. #5

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
http://smslib.org/forum/index.php?topic=41

Hello

We have found a problem with MessageEncoding.Enc8Bit encoding. Though we
are using version 2.1.4 the error must be also in the version 3.x because
the code is the same.

The bug is located in the method CMessage.setMessageEncoding when is passed
as argument the MessageEncoding.Enc8Bit value. In this case the method runs
the next lines intended to encode the sms as a string to another string in
ASCII-HEX format:

           case MessageEncoding.Enc8Bit:
               for (int i = 0; i < text.length(); i++)
               {
                   char c = text.charAt(i);;
                   encodedText += ((Integer.toHexString(c).length() < 2) ?
"0" + Integer.toHexString(c) : Integer.toHexString(c));
               }
               break;

The problem is that the incoming can be a binary message instead of text
message (this is our case). In this case, the 'charAt' method returns
unexpected values for some 'text' variable octects value. For instance, if
the byte value to encode is 128 (=0x80), after calling toHexString method
the result will be "20ac" instead of "80" (the expected value).

 The solution we have implemented is to use the next code instead of the above:

           byte[] bytes = text.getBytes();
           for (int i=0; i< bytes.length; i++)
           {
               String s = "0" + Integer.toHexString(bytes);
               int from = s.length()- 2;
               encodedText += s.substring(from);                         
          }

Original issue reported on code.google.com by T.Delenikas on 2 Aug 2007 at 5:30

GoogleCodeExporter commented 9 years ago

Original comment by T.Delenikas on 2 Aug 2007 at 5:34

GoogleCodeExporter commented 9 years ago
Fixed for SMSLib for Java v3.1 RC1

Original comment by T.Delenikas on 9 Nov 2007 at 9:52