嗨,我正在尝试解码以下字符串10410532119111114108100我刚刚用ascii编码,现在我也想将字符串转换为纯文本,我有以下代码:
int texto = 10410532119111114108100;
string resultado = "";
resultado = resultado + System.Convert.ToChar(texto);
Console.WriteLine(resultado);
但是不起作用,有人能帮我吗?
var asciiBytes = new byte[] { 104, 105, 32, 119, 111, 114, 108, 100 };
var text = System.Text.Encoding.ASCII.GetString(asciiBytes);
Console.WriteLine(text);
这将打印
hi world
抛开语言语法的问题不谈,代码中有一个基本问题。每个字符对应一个从0到255的ASCII代码。例如,"hi world"对应于104、105、32、119、111、114、108、100。如果删除单个代码之间的空格并创建一个长的数字串,可能有多种方法可以将其分解为单个代码。例如,10410532119111114108100可能来自原始序列,也可能来自{104,10,53,21,19…}或{10,4105,32,11,91…)等。因此,没有办法将没有空格的长字符串转换回字符。
我认为您正在将char
与char[]
混合。。。
首先,您的文本(texto
)是一个int
,但它太大了,无法开始。
其次,System.Convert.ToChar()
希望将某些内容转换为Unicode字符(只有1),因此向其传递一个从一开始就无效(按大小)的int是完全错误的。
看看ToChar,看看它是如何使用的。
假设你只是把一个字符串转换成char值,我试着把你的字符串分解成这样的东西:
var list = new List<int>{104,105,321,191,111,141,081,00};
foreach (var element in list)
{
Console.Out.WriteLine(Convert.ToChar(element));
}
// will output -> hiŁ¿oQ□ . Doubt it's what you've encoded though
您可以在这里找到一个简单的模式。我也遇到过同样的问题
这是ascii表:http://www.asciitable.com/index/asciifull.gif
我的模式并没有完全涵盖所有的可能性,但它是简单文本的解决方案。
这是代码:
string input = "10410532119111114108100";
string playground = input;
string result = "";
while (playground.Length > 0)
{
int temp = Convert.ToInt32(playground.Substring(0, 2));
if (temp < 32)
{
temp = Convert.ToInt32(playground.Substring(0, 3));
}
result += (Convert.ToChar(temp)).ToString();
playground = playground.Substring(temp.ToString().Length, playground.Length - temp.ToString().Length);
}