c# - 将字符串转换为 GSM
问题描述
使用它作为我应该能够期望段看起来像 https://twiliodeved.github.io/message-segment-calculator/的参考
这是我输入的字符串
Hello there stackoverflow this is a string that should be segmented into 2 segments for some reason my code only says 1 segment of 17 segments which is not right at all
我在这里尝试了两种不同的形式,第一种是
public static string ToGSM7(string text)
{
string gsm_table = "";
gsm_table += "@£$¥èéùìòÇ`Øø`Åå";
gsm_table += "Δ_ΦΓΛΩΠΨΣΘΞ`ÆæßÉ";
gsm_table += " !\"#¤%&'()*=,-./";
gsm_table += "0123456789:;<=>?";
gsm_table += "¡ABCDEFGHIJKLMNO";
gsm_table += "PQRSTUVWXYZÄÖÑÜ`";
gsm_table += "¿abcdefghijklmno";
gsm_table += "pqrstuvwxyzäöñüà";
string extendable_table = "";
extendable_table += "````````````````";
extendable_table += "````^```````````";
extendable_table += "````````{}`````\\";
extendable_table += "````````````[~]`";
extendable_table += "|```````````````";
extendable_table += "````````````````";
extendable_table += "`````€``````````";
extendable_table += "````````````````";
string gsm_output = "";
foreach (char c_text in text)
{
int int_gsm_table = extendable_table.IndexOf(text);
if (int_gsm_table != -1)
{
gsm_output += int_gsm_table.ToString("X2");
continue;
}
int int_extended_table = extendable_table.IndexOf(text);
if (int_extended_table != -1)
{
gsm_output += (27).ToString("X2");
gsm_output += int_extended_table.ToString("X2");
}
}
return gsm_output;
}
public static string GetSegments(string text)
{
int segments = 0;
if (ContainsEmoji(text))
{
string encoded_string = ToUCS2(text);
segments = (int)(encoded_string.Length / 67) + 1;
}
else
{
string encoded_string = ToGSM7(text);
segments = (int)(encoded_string.Length / 160) + 1;
}
return segments.ToString();
}
运行此命令后,无论我在字符串中放入多少文本,出于某种原因,我总是会返回 1 段
我尝试的第二种方法是(使用相同的 GetSegments 方法)
// 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;
}
这将导致 4 个段现在您可以说使用第二种方法我可以简单地将段分成两半以获得正确的数字,但事实并非如此
如果我例如制作字符串
Hello there stackoverflow this is a string that should be segmented into 2 segments for some reason my code only says 1 segment of 17 segments which is not right at allHello there stackoverflow this is a string that should be segmented into 2 segments for some reason my code only says 1 segment of 17 segments which is not right at all
然后在网站上,我将取回 3 个片段,使用我的代码,我将取回 7 个片段
解决方案
推荐阅读
- asp.net - IIS 8.5 配置导致 Sitefinity 12.1 动态链接创建问题 (ASP.NET .NET 4.7.2)
- bash - 使用 awk 有条件地对 jq 文件中的数字求和
- python - Django Rest Framework TypeError 需要一个类似字节的对象,而不是'str'
- amazon-web-services - 如何在多个子网上运行 AWS EMR 集群?
- datetime - 如何很好地将 Neo4J 日期映射到 dateTime 字符串?
- r - R:使用收集来清理数据集
- excel - 返回第一个单元格中具有特定值的两个单元格之间的差异
- rust - 如何在同一项目的同一目录中包含另一个文件中的文件?
- python - 根据内部字典中的条件获取特定键 - python
- python - Python – Simplex 为原始和对偶返回不同的值