C# 如何在c中以数字方式添加字符#
因此,我正在构建一个程序,它接受一个字符串并将其转换为二进制数(即“a”=01000001)。然后,如果用户愿意,它可以将该二进制数转换回ascii字符。代码如下:C# 如何在c中以数字方式添加字符#,c#,bin,C#,Bin,因此,我正在构建一个程序,它接受一个字符串并将其转换为二进制数(即“a”=01000001)。然后,如果用户愿意,它可以将该二进制数转换回ascii字符。代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Threading.Tasks; namespace NDR_011 { class Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
namespace NDR_011
{
class Program
{
public static Byte[] BinStr(String binary)
{
var list = new List<Byte>();
for (int i = 0; i < binary.Length; i += 8)
{
String t = binary.Substring(i, 8);
list.Add(Convert.ToByte(t, 2));
}
return list.ToArray();
}
public static void Print(object mess)
{
string tmp = mess.ToString().ToUpper();
Console.Write(tmp);
}
private static List<string> buffer = new List<string>();
private static string outfile = "C:/tmp/bytes.bin";
static void Main(string[] args)
{
string tmp = "";
Print("NDR 011\n");
while (true)
{
Print(""); tmp = Console.ReadKey().Key.ToString().ToUpper();
if (Console.CursorLeft > 0)
{
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
}
if (tmp == ConsoleKey.F1.ToString())
{
break;
} else if (tmp == ConsoleKey.F2.ToString())
{
comp();
continue;
} else if (tmp == ConsoleKey.F4.ToString())
{
buffer.Clear();
continue;
} else if (tmp == ConsoleKey.F5.ToString())
{
Print("N "); string a = Console.ReadLine();
outfile = a;
continue;
} else if (tmp == ConsoleKey.F5.ToString())
{
outfile = "C:/tmp/bytes.bin";
Print("Out file reset\n");
continue;
} else if (tmp == ConsoleKey.F7.ToString())
{
//Print("N "); // string a = Console.ReadLine();
string a = "C:/tmp/bytes.bin";
string[] s = File.ReadAllText(a).Split(' ');
char[] end = new char[s.Length - 1];
for (int i=0;i<end.Length;i++)
{
end[i] = (char)BinStr(s[i])[0];
//Print(end[i]);
}
//Print((char)BinStr(s[0])[0]);
if (end[0] == 'A' && end[1] == 'D' && end[2] == 'D')
{
for (int i=0+3;i<end.Length;i++)
{
int n = end[i] + end[i];
Print(n);
}
}
//decompile(a);
continue;
}
while (tmp.Length > 1)
{
char a = tmp[tmp.Length - 1];
tmp = a.ToString();
}
buffer.Add(tmp);
}
}
static void comp()
{
if (buffer == null || buffer.Count <= 0)
{
Print("Error buffer empty");
return;
}
char[] r = new char[buffer.Count];
for (int i=0;i<buffer.Count;i++)
{
r[i] = Convert.ToChar(buffer[i]);
Print(r[i]);
}
foreach (char ch in r)
{
string a = Convert.ToString((int)ch, 2);
while (a.Length != 8)
{
string b = "0";
a = b + a;
}
File.AppendAllText(outfile, a + " ");
}
Print("Compile done!\n");
}
static void decompile(string filename)
{
}
static void run()
{
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
使用System.Threading.Tasks;
名称空间NDR_011
{
班级计划
{
公共静态字节[]BinStr(字符串二进制)
{
var list=新列表();
对于(int i=0;i0)
{
Console.SetCursorPosition(Console.CursorLeft-1,Console.CursorTop);
}
if(tmp==ConsoleKey.F1.ToString())
{
打破
}else if(tmp==ConsoleKey.F2.ToString())
{
comp();
继续;
}else if(tmp==ConsoleKey.F4.ToString())
{
buffer.Clear();
继续;
}else if(tmp==ConsoleKey.F5.ToString())
{
打印(“N”);字符串a=Console.ReadLine();
outfile=a;
继续;
}else if(tmp==ConsoleKey.F5.ToString())
{
outfile=“C:/tmp/bytes.bin”;
打印(“输出文件重置\n”);
继续;
}else if(tmp==ConsoleKey.F7.ToString())
{
//Print(“N”);//字符串a=Console.ReadLine();
字符串a=“C:/tmp/bytes.bin”;
字符串[]s=File.ReadAllText(a).Split(“”);
char[]end=新字符[s.Length-1];
对于(int i=0;i以下是如何将字符串转换为1和0的二进制字符串:
var binstring = string.Join(" ", Encoding.ASCII.GetBytes(("Welcome, World!")).Select(byt => Convert.ToString(byt, 2).PadLeft(8, '0')));
要将其转换回字符串,我们需要使用如下方法从中解析byte[]数组:
public static byte[] GetBytes(string s)
{
byte[] result = new byte[(s.Length + 7) / 8];
int i = 0;
int j = 0;
foreach (char c in s)
{
result[i] <<= 1;
if (c == '1')
result[i] |= 1;
j++;
if (j == 8)
{
i++;
j = 0;
}
}
return result;
}
引用:
是否不允许使用内置类/结构来执行此操作?当您在调试器中逐步使用它时,您发现了什么?它首先在哪里出错?现在请小心,C#不使用ASCII来表示字符串
和字符
。如果您的函数仅对字符块有效,则在输入错误时引发异常不符合那个要求。
Encoding.ASCII.GetString(GetBytes(binstring));