пятница, 8 августа 2014 г.

C# модем и sms

Класс для декодирования sms-сообщения:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ManagmentSerialPort.Framework.SMS
{
    class SMSDecoder

    {

        Hashtable htAbc;

        public SMSDecoder()
        {
            htAbc = new Hashtable();
            htAbc.Add("0410", "А"); htAbc.Add("0411", "Б");
            htAbc.Add("0412", "В"); htAbc.Add("0413", "Г");
            htAbc.Add("0414", "Д"); htAbc.Add("0415", "Е");
            htAbc.Add("00A8", "Ё"); htAbc.Add("0416", "Ж");
            htAbc.Add("0417", "З"); htAbc.Add("0418", "И");
            htAbc.Add("0419", "Й"); htAbc.Add("041A", "К");
            htAbc.Add("041B", "Л"); htAbc.Add("041C", "М"); 
            htAbc.Add("041D", "Н"); htAbc.Add("041E", "О");
            htAbc.Add("041F", "П"); htAbc.Add("0420", "Р"); 
            htAbc.Add("0421", "С"); htAbc.Add("0422", "Т");
            htAbc.Add("0423", "У"); htAbc.Add("0424", "Ф"); 
            htAbc.Add("0425", "Х"); htAbc.Add("0426", "Ц");
            htAbc.Add("0427", "Ч"); htAbc.Add("0428", "Ш"); 
            htAbc.Add("0429", "Щ"); htAbc.Add("042C", "Ь");
            htAbc.Add("042A", "Ъ"); htAbc.Add("042D", "Э"); 
            htAbc.Add("042E", "Ю"); htAbc.Add("042F", "Я");
            htAbc.Add("0430", "а"); htAbc.Add("0431", "б"); 
            htAbc.Add("0432", "в"); htAbc.Add("0433", "г");
            htAbc.Add("0434", "д"); htAbc.Add("0435", "е"); 
            htAbc.Add("00B8", "ё"); htAbc.Add("0436", "ж");
            htAbc.Add("0437", "з"); htAbc.Add("0438", "и"); 
            htAbc.Add("0439", "й"); htAbc.Add("043A", "к");
            htAbc.Add("043B", "л"); htAbc.Add("043C", "м"); 
            htAbc.Add("043D", "н"); htAbc.Add("043E", "о");
            htAbc.Add("043F", "п"); htAbc.Add("0440", "р"); 
            htAbc.Add("0441", "с"); htAbc.Add("0442", "т"); 
            htAbc.Add("0443", "у"); htAbc.Add("0444", "ф"); 
            htAbc.Add("0445", "х"); htAbc.Add("0446", "ц");
            htAbc.Add("0447", "ч"); htAbc.Add("0448", "ш"); 
            htAbc.Add("0449", "щ"); htAbc.Add("044D", "э");
            htAbc.Add("044E", "ю"); htAbc.Add("044F", "я"); 
            htAbc.Add("0041", "A"); htAbc.Add("0042", "B"); 
            htAbc.Add("0043", "C"); htAbc.Add("0044", "D"); 
            htAbc.Add("0045", "E"); htAbc.Add("0046", "F");
            htAbc.Add("0047", "G"); htAbc.Add("0048", "H"); 
            htAbc.Add("0049", "I"); htAbc.Add("004A", "J");
            htAbc.Add("004B", "K"); htAbc.Add("004C", "L"); 
            htAbc.Add("004D", "M"); htAbc.Add("004E", "N");
            htAbc.Add("004F", "O"); htAbc.Add("0050", "P"); 
            htAbc.Add("0051", "Q"); htAbc.Add("0052", "R");
            htAbc.Add("0053", "S"); htAbc.Add("0054", "T"); 
            htAbc.Add("0055", "U"); htAbc.Add("0056", "V");
            htAbc.Add("0057", "W"); htAbc.Add("0058", "X"); 
            htAbc.Add("0059", "Y"); htAbc.Add("005A", "Z");
            htAbc.Add("0061", "a"); htAbc.Add("0062", "b"); 
            htAbc.Add("0063", "c"); htAbc.Add("0064", "d");
            htAbc.Add("0065", "e"); htAbc.Add("0066", "f"); 
            htAbc.Add("0067", "g"); htAbc.Add("0068", "h");
            htAbc.Add("0069", "i"); htAbc.Add("006A", "j"); 
            htAbc.Add("006B", "k"); htAbc.Add("006C", "l");
            htAbc.Add("006D", "m"); htAbc.Add("006E", "n"); 
            htAbc.Add("006F", "o"); htAbc.Add("0070", "p");
            htAbc.Add("0071", "q"); htAbc.Add("0072", "r"); 
            htAbc.Add("0073", "s"); htAbc.Add("0074", "t");
            htAbc.Add("0075", "u"); htAbc.Add("0076", "v"); 
            htAbc.Add("0077", "w"); htAbc.Add("0078", "x");
            htAbc.Add("0079", "y"); htAbc.Add("007A", "z"); 
            htAbc.Add("0030", "0"); htAbc.Add("0031", "1");
            htAbc.Add("0032", "2"); htAbc.Add("0033", "3"); 
            htAbc.Add("0034", "4"); htAbc.Add("0035", "5");
            htAbc.Add("0036", "6"); htAbc.Add("0037", "7"); 
            htAbc.Add("0038", "8"); htAbc.Add("0039", "9");
            htAbc.Add("0027", "'"); htAbc.Add("002D", "-"); 
            htAbc.Add("002A", "*"); htAbc.Add("0020", " ");
            htAbc.Add("003A", ":"); htAbc.Add("003B", ";"); 
            htAbc.Add("0029", ")"); htAbc.Add("0028", "(");
            htAbc.Add("002E", "."); htAbc.Add("002C", ","); 
            htAbc.Add("0021", "!"); htAbc.Add("003D", "=");
            htAbc.Add("005F", "_"); htAbc.Add("044C", "ь"); 
            htAbc.Add("002B", "+");
        }

        public string DecodeMessage(string sMessage)
        {

            string str = "";
            int n = 4;
            List<string> lst = new List<string>();

            for (int i = 0; i < sMessage.Length; i += n)
            {
                lst.Add(sMessage.Substring(i, n));
            }

            foreach (string code in lst) {
                str += GetCharByCode(code);
            }
            return str;
        }

        private string GetCharByCode(string code)
        {

            string retVal = "";
            try
            {
                retVal = htAbc[code].ToString();
            } catch (Exception ex) {
                // System.Windows.Forms.MessageBox.Show("Отсутствует сопоставление для кода " + code);
                return "?";
            }
            return retVal;
        }
    }
}


Использование класса:

    ...
    SMSDecoder smsDecoder;

    smsDecoder = new Framework.SMS.SMSDecoder();

    MessageBox.Show(smsDecoder.DecodeMessage( "041D043E043C04350440002004320430044804350433043E0020043C043E04310438043B044C043D043E0433043E002004420435043B04350444043E043D04300020002B00370039003800390036003800320030003200330039"));
    ....

В выводимом сообщении мы получаем раскодированный текст "Номер вашего мобильного телефона +79896820239" 

Комментариев нет:

Отправить комментарий

Docker. Первые контейнеры

Источник:  php.dragomano.ru Структура каталогов: ~/develop/web/project/                  nginx/      - сервис web-сервера                php...