C# 从文本文件中读取行,转换行,然后写回新文件
我有一些C#的基本知识,但在编写一些概念上看似简单的代码时遇到了困难。我想读取包含以下值的文件(.asm):C# 从文本文件中读取行,转换行,然后写回新文件,c#,binary,C#,Binary,我有一些C#的基本知识,但在编写一些概念上看似简单的代码时遇到了困难。我想读取包含以下值的文件(.asm): @1 @12 @96 @2 @46 etc. 在多个连续的行上。然后我想去掉@符号(如果它们存在),将剩余的数值转换成二进制,然后将这些二进制值写回它们自己的行上的新文件(.hack)。对行数没有设置限制,这是我最大的问题,因为我不知道如何动态检查行。到目前为止,如果我编写代码查找这些行,我只能读取和转换这些行,那么我就不知道如何在新文件中自己的行中写入这些值。抱歉,如果这听起来有点复
@1
@12
@96
@2
@46
etc.
在多个连续的行上。然后我想去掉@符号(如果它们存在),将剩余的数值转换成二进制,然后将这些二进制值写回它们自己的行上的新文件(.hack)。对行数没有设置限制,这是我最大的问题,因为我不知道如何动态检查行。到目前为止,如果我编写代码查找这些行,我只能读取和转换这些行,那么我就不知道如何在新文件中自己的行中写入这些值。抱歉,如果这听起来有点复杂,但任何帮助将不胜感激。谢谢
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
var line = File.ReadAllText(openFileDialog1.FileName);
using (StreamWriter sw = File.CreateText("testCode.hack"))
{
var str = line;
var charsToRemove = new string[] {"@"};
foreach (var c in charsToRemove)
{
str = str.Replace(c, string.Empty);
}
int value = Convert.ToInt32(str);
string value2 = Convert.ToString(value, 2);
if (value2.Length < 16)
{
int zeroes = 16 - value2.Length;
if(zeroes == 12)
{
sw.WriteLine("000000000000" + value2);
}
}
else
{
sw.WriteLine(value2);
}
}
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
var line=File.ReadAllText(openFileDialog1.FileName);
使用(StreamWriter sw=File.CreateText(“testCode.hack”))
{
var-str=line;
var charsToRemove=新字符串[]{“@”};
foreach(charsToRemove中的变量c)
{
str=str.Replace(c,string.Empty);
}
int value=Convert.ToInt32(str);
字符串值2=Convert.ToString(值2);
如果(值2.长度<16)
{
整数零=16-值2.长度;
如果(零=12)
{
软件写入线(“000000000000”+值2);
}
}
其他的
{
软件写入线(值2);
}
}
我的建议创建一个列表
将输入(.asm)文件读取到列表中
打开输出(.hack)文件的StreamWriter
循环查看列表
修改字符串并写入文件
代码示例:
List<string> lstInput = new List<string>();
using (StreamReader reader = new StreamReader(@"input.asm"))
{
string sLine = string.Empty;
//read one line at a time
while ((sLine = reader.ReadLine()) != null)
{
lstInput.Add(sLine);
}
}
using (StreamWriter writer = new StreamWriter(@"output.hack"))
{
foreach(string sFullLine in lstInput)
{
string sNumber = sFullLine;
//remove leading @ sign
if(sFullLine.StartsWith("@"))
sNumber = sFullLine.Substring(1);
int iNumber;
if(int.TryParse(sNumber, out iNumber))
{
writer.WriteLine(IntToBinaryString(iNumber));
}
}
}
public string IntToBinaryString(int number)
{
const int mask = 1;
var binary = string.Empty;
while(number > 0)
{
// Logical AND the number and prepend it to the result string
binary = (number & 1) + binary;
number = number >> 1;
}
return binary;
}
List lstInput=new List();
使用(StreamReader=newstreamreader(@“input.asm”))
{
string sLine=string.Empty;
//一次读一行
而((sLine=reader.ReadLine())!=null)
{
lstInput.Add(sLine);
}
}
使用(StreamWriter=newstreamwriter(@“output.hack”))
{
foreach(输入中的字符串sfulllline)
{
字符串sNumber=sFullLine;
//删除前导@符号
if(sFullLine.StartsWith(“@”))
sNumber=sFullLine.Substring(1);
国际单位;
if(内锥巴色(sNumber,out iNumber))
{
writer.WriteLine(IntToBinaryString(iNumber));
}
}
}
公共字符串IntToBinaryString(整数编号)
{
常数int mask=1;
var binary=string.Empty;
而(数量>0)
{
//逻辑和数字,并将其前置到结果字符串
二进制=(数字和1)+二进制;
数字=数字>>1;
}
返回二进制;
}
参考:方法
注意:在@TheDutchMan的答案中提到的Int-to-Binary-String方法是更好的选择。这段代码应该可以帮助您快速完成:
static void Main(string[] args)
{
string line = string.Empty;
System.IO.StreamReader reader = new System.IO.StreamReader(@"C:\test.txt");
System.IO.StreamWriter writer = new System.IO.StreamWriter(@"C:\test.hack");
while ((line = reader.ReadLine()) != null) // Read until there is nothing more to read
{
if (line.StartsWith("@"))
{
line = line.Remove(0, 1); // Remove '@'
}
int value = -1;
if (Int32.TryParse(line, out value)) // Check if the rest string is an integer
{
// Convert the rest string to its binary representation and write it to the file
writer.WriteLine(intToBinary(value));
}
else
{
// Couldn't convert the string to an integer..
}
}
reader.Close();
writer.Close();
Console.WriteLine("Done!");
Console.Read();
}
//http://www.dotnetperls.com/binary-representation
static string intToBinary(int n)
{
char[] b = new char[32];
int pos = 31;
int i = 0;
while (i < 32)
{
if ((n & (1 << i)) != 0)
{
b[pos] = '1';
}
else
{
b[pos] = '0';
}
pos--;
i++;
}
return new string(b);
}
static void Main(字符串[]args)
{
string line=string.Empty;
System.IO.StreamReader=new System.IO.StreamReader(@“C:\test.txt”);
System.IO.StreamWriter writer=新的System.IO.StreamWriter(@“C:\test.hack”);
while((line=reader.ReadLine())!=null)//读取,直到没有更多内容可读取为止
{
if(第行开始时使用(“@”))
{
line=line.Remove(0,1);//Remove'@'
}
int值=-1;
if(Int32.TryParse(line,out value))//检查rest字符串是否为整数
{
//将rest字符串转换为其二进制表示形式并将其写入文件
writer.WriteLine(intToBinary(value));
}
其他的
{
//无法将字符串转换为整数。。
}
}
reader.Close();
writer.Close();
控制台。WriteLine(“完成!”);
Console.Read();
}
//http://www.dotnetperls.com/binary-representation
静态字符串intToBinary(int n)
{
char[]b=新字符[32];
int pos=31;
int i=0;
而(i<32)
{
如果(n&(1)您能提供一些您已经尝试过的代码吗?您可以从这里开始。我添加了一些代码,但现在它只读取、转换和写入文件中的一行,并且一次不能处理多个值。我会将ASCII转换为十六进制数组。删除十六进制中@的任何值,然后分离。我还会考虑使用一些常规的表达式,如果您希望逐行动态执行。此外,我尝试实现File.ReadAllLines,但我不确定如何以易于转换的方式保存它读取的所有内容。这可以在数组或某种列表中完成吗?再加上一个using关键字。非常感谢!这正是我所需要的,我将使用I当我扩展这个项目时,我将不再依赖它。