Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C将字符串转换为GSM 7位#_C#_Gsm - Fatal编程技术网

C# 使用C将字符串转换为GSM 7位#

C# 使用C将字符串转换为GSM 7位#,c#,gsm,C#,Gsm,如何将字符串转换为正确的GSM编码值以发送给移动运营商?下面是一个端口,稍作修改并有详细说明 例如: 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 clas

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

下面是一个端口,稍作修改并有详细说明

例如:

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;
    }
}
//数据/信息取自http://en.wikipedia.org/wiki/GSM_03.38
公共静态类GSMConverter
{
//字符串中字符的索引表示索引
//在各自的字符集中的字符
//基本字符集
私有常量字符串基本集合=
“@$$èùòòòòòòòòùùùùùùùùùùùùùùùùùùù+
“《基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基辅基;
//基本字符集扩展
私有常量字符串扩展集=
"````````````````````^```````````````````{}`````\\````````````[~]`" +
"|````````````````````````````````````€``````````````````````````";
//如果字符在扩展集中,则必须在其前面
//使用基本字符集中索引为“27”的“ESC”字符
私有常量int ESC_INDEX=27;
公共静态字符串StringToGSMHexString(字符串文本,bool differenceWithDash=true)
{
//用替换\r\n以减少字符数
text=text.Replace(Environment.NewLine,“\r”);
//使用此列表可在中存储字符的索引
//基本/扩展字符集
变量指标=新列表();
foreach(文本中的var c)
{
int index=基本集合IndexOf(c);
如果(索引!=-1){
添加(索引);
继续;
}
index=扩展集IndexOf(c);
如果(索引!=-1){
//在添加之前添加“ESC”字符索引
//扩展字符索引
添加(ESC_索引);
添加(索引);
继续;
}
}
//将标记转换为2位十六进制
var hex=标记。选择(i=>i.ToString(“X2”)).ToArray();
字符串分隔符=用破折号分隔?”-“:”;
//定界输出
string delimited=string.Join(分隔符,十六进制);
返回分隔符;
}
}

奥马尔的代码对我不起作用。但我找到了真正起作用的代码:

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();
        }
公共静态字符串Encode7bit(字符串s)
{
string empty=string.empty;
对于(int index=s.Length-1;index>=0;--index)
empty+=Convert.ToString((字节)s[index],2).PadLeft(8,'0').Substring(1);
字符串str1=empty.PadLeft((int)Math.天花((Decimal)empty.Length/new Decimal(8))*8,“0”);
List byteList=新列表();
while(str1!=string.Empty)
{
字符串str2=str1.Substring(0,str1.Length>7?8:str1.Length).PadRight(8,'0');
str1=str1.Length>7?str1.Substring(8):string.Empty;
添加(Convert.ToByte(str2,2));
}
byteList.Reverse();
var messageBytes=byteList.ToArray();
var encodedData=“”;
foreach(messageBytes中的字节b)
{
encodedData+=Convert.ToString(b,16).PadLeft(2,'0');
}
返回encodedData.ToUpper();
}

您可能还想在上查看python实现。尝试使用确认您的转换。我已将基本集中位置5F处的
`
字符更改为
§
,这是它的正确值