c# - 将二进制字符的十进制值转换为字符
问题描述
我正在将消息转换为 ascii 十进制值的 oa 项目......这一面并不重要,问题是它需要读回来所以翻译基本上是这样的:
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”,所以 pc 弄错了。那是我的问题,我需要你的帮助:)。我有点菜鸟,很抱歉我的错误,我 17 岁,英语也不是我的母语。注意:ascii 值可能不是真实的,这些只是举例。
解决方案
你的代码有很多问题。检查Contains
将在任何位置出现任意数量的字符时返回 true。您正在签入textBox1
并更换textBox3
. 您正在检查您知道的每个字符,但可能还有更多!根据输入的编码,有更简单的方法可以获取字符的字节/整数/数字等价物。
这是基于问题后评论的基本解决方案。但是,您需要阅读有关代码页和编码的更多信息。这只是加密操作的一部分。我相信您可以弄清楚如何替换内容,然后还可以解密为可用格式。干杯! 快乐编码。
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;
}
推荐阅读
- r - 如何保持数据文件中相关图标签的顺序相同?
- lua - Lua中的快速序列化/反序列化表(无递归)
- d3.js - 存储多个 (~10000) SVG 填充模式
- jsp - 在第 39 行处理 JSP 页面 /index.jsp 时发生异常
- swift - 在 Metal 着色器中写入 0-Alpha 无法正确渲染
- java - 无法在 Raspberry Pi 3(Raspbian)上运行我的 JavaFX jar 文件
- android-fragments - 如何处理工具栏中的向上按钮 - Kotlin
- function - Modern Fortran 中二阶微分方程的通用 runge-kutta 函数
- javascript - 使用 JS 的数组映射 - 将值与另一个数组进行比较并从第二个数组返回值
- regex - 匹配除正斜杠以外的任何字符