Friday, April 15, 2011

Encoding £ in SMS message sent via Gateway not working correctly

Hi,

I'm having trouble with an SMS message I am sending using a provider called Cymba.

Basically I'm posting the message over to them as per their specifications URL encoded.

However when the message is sent to my phone the £ appears with an A in front i.e. £A

Now C# strings are Unicode by default, so I switched to ASCII encoding and the £ comes over as ?.

I've tried converting the message string to hex starting form the Unicode and ASCII versions (as via this gateway you an also submit the message as hex). However the same behaviour occurs.

I suspect it is something to do with the string encoding as SMS supports a 7bit encoding format: GSM 03.38

I suspect also that if I send over Unicode I'll only get a 70 or 140 char message instead of the standard 160.

I'm quite new to SMS sending so any help or advice will be appreciated.

So the question is, how can I get my C# strings into this format?

Update: Looks like the Gateway is the one with the problem not my code as I tried replacing the offending £ with the 7bit equivalent hex value and this did not work either. See below for my test code which may help others in future:

byte pound = 0xA3;
byte GSMPound = 0x01;
string hexxedMsg = "";
System.Text.UnicodeEncoding encoder = new System.Text.UnicodeEncoding();
byte[] msgBytes = encoder.GetBytes(message);

foreach (byte byt in msgBytes)
{
    if(byt == pound)
        hexxedMsg += GSMPound.ToString("X");
    else
        hexxedMsg += byt.ToString("X2"); ;
}
From stackoverflow
  • There's a GSM 03.38 to Unicode mapping table available which suggests that £ should be encoded as 0x01 so you could try that (just hard-coding it for a single attempt). However, you should really check all of this with the gateway operator. Is the gateway genuinely expecting you to post in URL-encoded GSM 03.38? If so, that mapping table is probably the way to go - you may need to write your own implementation to map from a string to bytes (fully implementing System.Text.Encoding would be overkill). I'd try it with a hard-coded message first though.

    Tim Saunders : Having done some experimenting I've decided to accept your answer as having gone to the effort of testing sending of a 7bit encoded message I've found the gateway is the one with the problem. So your answer is closest to the solution.
  • This page contains info on the "7-bit default alphabet".

    The simplest solution may be to create a Dictionary that maps the Unicode code points encoded by the GSM 03.38 standard (approximately 140 glyphs) into 1 or 2 bytes (2 bytes are needed for the 0x27, 0x?? pairs).

    Once that's done (depending on your provider) you may also have to squeeze the 8-bit bytes into a stream of 7-bit sequences. There's a visual sample here.

    And after you've achieved all of that (and had a couple of beers!) you might want to turn it into a System.Text.Encoding implementation.


    You might also want to have a look at PDUDecoder on CodeProject for the reverse operation. There's an online version too.

0 comments:

Post a Comment