[英]Display a text file in two boxes, one in plain text and the other converted to hex
I have a form with two text boxes, in one I want to show a .txt file in text format and in the other text box I want to show the same text but in hex. 我有一个带有两个文本框的表单,一个表单中我想以文本格式显示.txt文件,另一个文本框中我想以十六进制显示相同的文本。 I have no idea how to do this as programming isn't a strong point of mine.
我不知道该怎么做,因为编程不是我的强项。 Also it has to be in whole columns and the hex box should show the value of each character in the form
0xNN
. 同样,它必须在整列中,并且十六进制框应以
0xNN
的形式显示每个字符的值。 In case anyone recognizes this I did ask a similar question earlier today, but have since progressed with some of it. 万一有人意识到这一点, 我今天早些时候确实提出了类似的问题 ,但此后有所进展。 I'm using Visual C# Express Edition 2008 obviously dealing with C#.
我正在使用Visual C#Express Edition 2008,显然是在处理C#。
StringBuilder sb = new StringBuilder();
foreach (char character in File.ReadAllText("input.txt").ToCharArray())
{ //convert the string to array of bytes
sb.Append("0x"+ ///"0x" prefix
((int)character). //convert char to int
ToString("X2")); //generate string with two hex digit.
sb.Append("\n"); //new line after each converted char
}
TextBox1.Text = sb.ToString(); //set text box text
The easiest way would probably be to convert the text to a byte[], 最简单的方法可能是将文本转换为byte [],
byte[] bytes = Encoding.ASCII.GetBytes(textFromFile);
and use BitConverter.ToString()
to build a hex list of the byte[]. 并使用
BitConverter.ToString()
构建byte []的十六进制列表。
string hexListing = BitConverter.ToString(bytes)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.