GSM 03.38 support
Posted: Tue Nov 10, 2009 3:14 pm
Is there support for GSM 03.38 encoding or do I need to write my own?
Code: Select all
req.ShortMessageBytes = GSMEncode("!£$%^&*()€");
public static byte[] GSMEncode(string PlainText)
{
// ` is not a conversion, just a untranslatable letter.
// x0D should be CR but this causes double linefeed on
// some phones so has been removed.
const string strGSMTable =
"@£$¥èéùìòÇ\nØø`Åå" +
"Δ_ΦΓΛΩΠΨΣΘΞ`ÆæßÉ" +
" !\"#¤%&'()*+,-./" +
"0123456789:;<=>?" +
"¡ABCDEFGHIJKLMNO" +
"PQRSTUVWXYZÄÖÑܧ" +
"¿abcdefghijklmno" +
"pqrstuvwxyzäöñüà";
const string strExtendedTable =
"````````````````" +
"````^```````````" +
"````````{}`````\\" +
"````````````[~]`" +
"|```````````````" +
"````````````````" +
"`````€``````````" +
"````````````````";
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
foreach (char cPlainText in PlainText.ToCharArray())
{
if (cPlainText != '`') // Ignore `
{
int intGSMTable = strGSMTable.IndexOf(cPlainText);
if (intGSMTable != -1)
{
ms.WriteByte((byte)intGSMTable);
continue;
}
int intExtendedTable = strExtendedTable.IndexOf(cPlainText);
if (intExtendedTable != -1)
{
ms.WriteByte(27);
ms.WriteByte((byte)intExtendedTable);
}
}
}
ms.Close();
return ms.ToArray();
}
}
Code: Select all
public static string GsmDecode(byte[] encodedData)
{
// ? is not a conversion, just a untranslatable letter.
const string gsmTable =
"@£$¥èéùìòÇ\nØø\rÅå" +
"Δ_ΦΓΛΩΠΨΣΘΞ`ÆæßÉ" +
" !\"#¤%&'()*+,-./" +
"0123456789:;<=>?" +
"¡ABCDEFGHIJKLMNO" +
"PQRSTUVWXYZÄÖÑܧ" +
"¿abcdefghijklmno" +
"pqrstuvwxyzäöñüà";
const string extendedTable =
"????????????????" +
"????^???????????" +
"????????{}?????\\" +
"????????????[~]?" +
"|???????????????" +
"????????????????" +
"?????€??????????" +
"????????????????";
StringBuilder retVal = new StringBuilder();
for (int i = 0; i < encodedData.Length; i++)
{
// if escape code,
if (encodedData[i] == 27)
{
retVal.Append(extendedTable[encodedData[++i]]);
}
else
{
retVal.Append(gsmTable[encodedData[i]]);
}
}
return retVal.ToString();
}