将二进制字符的十进制值转换为字符
Turning the decimal values of characters in binary into character
我正在做一个将消息转换为 ascii 十进制值的项目...这边并不重要,问题是它需要读回来所以翻译基本上是这样的:
if (textBox1.Text.Contains("a"))
{
textBox3.Text = textBox3.Text.Replace("a", "97");
}
if (textBox1.Text.Contains("b"))
{
textBox3.Text = textBox3.Text.Replace("b", "98");
}
.
.
.
if (textBox1.Text.Contains("Ğ"))
{
textBox3.Text = textBox3.Text.Replace("Ğ", "286");
}
if (textBox1.Text.Contains("ş"))
{
textBox3.Text = textBox3.Text.Replace("ş", "351");
}
这个翻译很完美。
但翻译回输出是问题所在。
我的翻译方法简而言之:
if (sonmesajBinary.Text.Contains("97"))
{
okunanMesaj.Text = okunanMesaj.Text.Replace("97", "a");
}
if (sonmesajBinary.Text.Contains("98"))
{
okunanMesaj.Text = okunanMesaj.Text.Replace("98", "b");
}
if (sonmesajBinary.Text.Contains("99"))
{
okunanMesaj.Text = okunanMesaj.Text.Replace("99", "c");
}
问题是假设输出是 140
但它也包括“40”
所以电脑弄错了。那是我的问题,我需要您的帮助:)。
我有点菜鸟,很抱歉我的错误,我 17 岁,英语也不是我的母语。
注意:ascii 值可能不是真实值,这些只是示例。
感谢 Tom Blodget,它已修复,我需要做的就是定界。所以我在每2位数字的开头添加了0 values:D
if (textBox1.Text.Contains("a"))
{
textBox3.Text = textBox3.Text.Replace("a", "097");
}
你的代码有很多问题。检查 Contains
将 return 对于在任何位置出现的任意次数的字符为真。您正在签入 textBox1
并在 textBox3
中替换。您正在检查您知道的每个角色,但可能还有更多!有更简单的方法可以根据输入的编码获取 byte/int/number 等效的字符。
这是一个基于问题后评论的基本解决方案。但是,您需要阅读有关代码页和编码的更多信息。这只是 Encrypt 操作的一部分。我相信您可以弄清楚如何替换内容,然后再 Decrypt 到可用格式。干杯!编码愉快。
static void Main(string[] args)
{
string fileContents = "";
int encryptKey = 3; // Consider getting this from args[0], etc.
using (FileStream fs = File.OpenRead(@"C:\Users\My\Desktop\testfile.txt"))
using (TextReader tr = new StreamReader(fs))
{
fileContents = tr.ReadToEnd();
}
byte[] asciiBytesOfFile = Encoding.ASCII.GetBytes(fileContents);
int[] encryptedContents = Encrypt(encryptKey, asciiBytesOfFile);
}
private static int[] Encrypt(int encryptKey, byte[] asciiBytesOfFile)
{
int[] encryptedChars = new int[asciiBytesOfFile.Length];
for (int i = 0; i < asciiBytesOfFile.Length; i++)
{
encryptedChars[i] = encryptKey ^ asciiBytesOfFile[i];
}
return encryptedChars;
}
我正在做一个将消息转换为 ascii 十进制值的项目...这边并不重要,问题是它需要读回来所以翻译基本上是这样的:
if (textBox1.Text.Contains("a"))
{
textBox3.Text = textBox3.Text.Replace("a", "97");
}
if (textBox1.Text.Contains("b"))
{
textBox3.Text = textBox3.Text.Replace("b", "98");
}
.
.
.
if (textBox1.Text.Contains("Ğ"))
{
textBox3.Text = textBox3.Text.Replace("Ğ", "286");
}
if (textBox1.Text.Contains("ş"))
{
textBox3.Text = textBox3.Text.Replace("ş", "351");
}
这个翻译很完美。 但翻译回输出是问题所在。 我的翻译方法简而言之:
if (sonmesajBinary.Text.Contains("97"))
{
okunanMesaj.Text = okunanMesaj.Text.Replace("97", "a");
}
if (sonmesajBinary.Text.Contains("98"))
{
okunanMesaj.Text = okunanMesaj.Text.Replace("98", "b");
}
if (sonmesajBinary.Text.Contains("99"))
{
okunanMesaj.Text = okunanMesaj.Text.Replace("99", "c");
}
问题是假设输出是 140 但它也包括“40” 所以电脑弄错了。那是我的问题,我需要您的帮助:)。 我有点菜鸟,很抱歉我的错误,我 17 岁,英语也不是我的母语。 注意:ascii 值可能不是真实值,这些只是示例。
感谢 Tom Blodget,它已修复,我需要做的就是定界。所以我在每2位数字的开头添加了0 values:D
if (textBox1.Text.Contains("a"))
{
textBox3.Text = textBox3.Text.Replace("a", "097");
}
你的代码有很多问题。检查 Contains
将 return 对于在任何位置出现的任意次数的字符为真。您正在签入 textBox1
并在 textBox3
中替换。您正在检查您知道的每个角色,但可能还有更多!有更简单的方法可以根据输入的编码获取 byte/int/number 等效的字符。
这是一个基于问题后评论的基本解决方案。但是,您需要阅读有关代码页和编码的更多信息。这只是 Encrypt 操作的一部分。我相信您可以弄清楚如何替换内容,然后再 Decrypt 到可用格式。干杯!编码愉快。
static void Main(string[] args)
{
string fileContents = "";
int encryptKey = 3; // Consider getting this from args[0], etc.
using (FileStream fs = File.OpenRead(@"C:\Users\My\Desktop\testfile.txt"))
using (TextReader tr = new StreamReader(fs))
{
fileContents = tr.ReadToEnd();
}
byte[] asciiBytesOfFile = Encoding.ASCII.GetBytes(fileContents);
int[] encryptedContents = Encrypt(encryptKey, asciiBytesOfFile);
}
private static int[] Encrypt(int encryptKey, byte[] asciiBytesOfFile)
{
int[] encryptedChars = new int[asciiBytesOfFile.Length];
for (int i = 0; i < asciiBytesOfFile.Length; i++)
{
encryptedChars[i] = encryptKey ^ asciiBytesOfFile[i];
}
return encryptedChars;
}