-4

I need to decode GSM 7 bit to ascii string in c# so that I googled and found lots of different posts about it, this post is one of them but It is not a c#.

Can anyone please share a c# code that can decode GSM 7-bit Character to ASCII string.

Thanks.

abdikadir
  • 1
  • 6
  • What have you tried yourself? I don’t know Python myself but the code you linked (especially the accepted answer) seems very straightforward and shouldn’t be that much of a problem to convert to C#. – ckuri Mar 02 '19 at 09:56
  • I tried to convert it to c# but it did not give the expected result. – abdikadir Mar 02 '19 at 11:05
  • Then edit your question and show what you have done/post your C# code. – ckuri Mar 02 '19 at 11:07
  • I followed a very easy flowchart and I finally come to solve my question. Thanks @ckuri – abdikadir Mar 05 '19 at 11:21

1 Answers1

0
  class GSM7BitDecoder
{
    // Basic Character Set
    private const string BASIC_SET =
            "@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞ\x1bÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?" +
            "¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà";

    // Basic Character Set Extension 
    private const string EXTENSION_SET =
            "````````````````````^```````````````````{}`````\\````````````[~]`" +
            "|````````````````````````````````````€``````````````````````````";


    string[] BASIC_SET_ARRAY = BASIC_SET.Select(x => x.ToString()).ToArray();
    string[] EXTENSION_SET_ARRAY = EXTENSION_SET.Select(x => x.ToString()).ToArray();

    enum circle { Start=1, Complete=8 }

    string GetChar(string bin)
    {
        try
        {
            if (Convert.ToInt32(bin, 2).Equals(27))
              return EXTENSION_SET_ARRAY[Convert.ToInt32(bin, 2)];
            else
                return BASIC_SET_ARRAY[Convert.ToInt32(bin, 2)];
        }
        catch { return string.Empty; }
    }
    public string DecodeGSM7bit(string strGsm7bit)
    {

        var suffix = string.Empty;
        var septet = string.Empty;
        var CurSubstr = string.Empty;
        var counter = 1;
        List<string> septets = new List<string>();
        List<string> sectets = new List<string>();

        //Prepare Octets
        var octets = Enumerable.Range(0, strGsm7bit.Length / 2).Select(i => 
        {
           return Convert.ToString(Convert.ToInt64(strGsm7bit.Substring(i * 2, 2), 16), 2).PadLeft(8,'0');

        }).ToList();


        for (var index=0; index < octets.Count; index = index +1)
        {
            //Generate Septets
            septet = octets[index].Substring(counter);
            CurSubstr = octets[index].Substring(0, counter);

            if (counter.Equals((int)circle.Start))
                septets.Add(septet);
            else
                septets.Add(septet + suffix);

            //Organize Sectets
            sectets.Add(GetChar(septets[index]));

            suffix = CurSubstr;
            counter++;

            //Reset counter when the circle is complete.
            if (counter == (int)circle.Complete)
            {
                counter = (int)circle.Start;
                sectets.Add(GetChar(suffix));
            }

        }
        return string.Join("", sectets);
    }
abdikadir
  • 1
  • 6