【问题标题】:Convert string to GSM 7-bit using C#使用 C# 将字符串转换为 GSM 7 位
【发布时间】:2013-06-20 16:26:59
【问题描述】:

如何将字符串转换为正确的 GSM 编码值以发送给移动运营商?

【问题讨论】:

    标签: c# gsm


    【解决方案1】:

    下面是gnibbler's answer的端口,稍作修改并附有详细解释。

    例子:

    string output = GSMConverter.StringToGSMHexString("Hello World");
    // output = "48-65-6C-6C-6F-20-57-6F-72-6C-64"
    

    实施:

    // Data/info taken from http://en.wikipedia.org/wiki/GSM_03.38
    public static class GSMConverter
    {
        // The index of the character in the string represents the index
        // of the character in the respective character set
    
        // Basic Character Set
        private const string BASIC_SET = 
                "@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞ\x1bÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?" +
                "¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà";
    
        // Basic Character Set Extension 
        private const string EXTENSION_SET =
                "````````````````````^```````````````````{}`````\\````````````[~]`" +
                "|````````````````````````````````````€``````````````````````````";
    
        // If the character is in the extension set, it must be preceded
        // with an 'ESC' character whose index is '27' in the Basic Character Set
        private const int ESC_INDEX = 27;
    
        public static string StringToGSMHexString(string text, bool delimitWithDash = true)
        {
            // Replace \r\n with \r to reduce character count
            text = text.Replace(Environment.NewLine, "\r");
    
            // Use this list to store the index of the character in 
            // the basic/extension character sets
            var indicies = new List<int>();
    
            foreach (var c in text)
            {
                int index = BASIC_SET.IndexOf(c);
                if(index != -1) {
                    indicies.Add(index);
                    continue;
                }
    
                index = EXTENSION_SET.IndexOf(c);
                if(index != -1) {
                    // Add the 'ESC' character index before adding 
                    // the extension character index
                    indicies.Add(ESC_INDEX);
                    indicies.Add(index);
                    continue;
                }
            }
    
          // Convert indicies to 2-digit hex
          var hex = indicies.Select(i => i.ToString("X2")).ToArray();
    
          string delimiter = delimitWithDash ? "-" : "";
    
          // Delimit output
          string delimited = string.Join(delimiter, hex);
          return delimited;
        }
    }
    

    【讨论】:

    【解决方案2】:

    Omar 的代码对我不起作用。但我发现了有效的实际代码:

    public static string Encode7bit(string s)
            {
                string empty = string.Empty;
                for (int index = s.Length - 1; index >= 0; --index)
                    empty += Convert.ToString((byte)s[index], 2).PadLeft(8, '0').Substring(1);
                string str1 = empty.PadLeft((int)Math.Ceiling((Decimal)empty.Length / new Decimal(8)) * 8, '0');
                List<byte> byteList = new List<byte>();
                while (str1 != string.Empty)
                {
                    string str2 = str1.Substring(0, str1.Length > 7 ? 8 : str1.Length).PadRight(8, '0');
                    str1 = str1.Length > 7 ? str1.Substring(8) : string.Empty;
                    byteList.Add(Convert.ToByte(str2, 2));
                }
                byteList.Reverse();
                var messageBytes = byteList.ToArray();
                var encodedData = "";
                foreach (byte b in messageBytes)
                {
                    encodedData += Convert.ToString(b, 16).PadLeft(2, '0'); 
                }
                return encodedData.ToUpper();
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-20
      • 2020-10-11
      • 2011-11-15
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多