在C#中阅读Bynary [英] Reading Bynary in C#
本文介绍了在C#中阅读Bynary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个文本文件,编码为 ASCII
。它包含以下文字;
I''ve created a text file Encoded in ASCII
. It Contains the Following Text;
abcd123
以上文字的二进制值;
Binary value of the above text ;
01100001 01100010 01100011 01100100 0110001 0110010 0110011
如何在C#中执行此操作(将文件的二进制值转换为文本框)?
How Can I do this (getting binary value of a file to a text box) in C# ?
推荐答案
阅读文本很简单: MSDN [< a href =http://www.google.co.uk/url?sa=t&rct=j&q=file.readallbytes&source=web&cd=1&ved=0CB8QFjAA&url=http%3A% 2F%2Fmsdn.microsoft.com%2Fen-我们%2Flibrary%2Fsystem.io.file.readallbytes.aspx&安培; EI = rDrbTreaMMWa8QPzubH_Bg&安培; USG = AFQjCNEn9JSi6FIXHOmqVNQdbZU NT60krQ& sig2 = iq-MtDQSLaOYzdyZ9Tjd5Qtarget =_ blanktitle =New Window> ^ ]
Reading text is easy: MSDN[^]
byte[] data = File.ReadAllBytes(path);
Converting to binary is easy: MSDN[^]
string s = Convert.ToString(myByte, 2);
将它们与foreach循环和StringBuilder放在一起,你就有了一个程序...
但是,既然这是你的功课,我就把实际的代码留给你了! :笑:
Put them together with a foreach loop and a StringBuilder and you have a program...
But, since this is your homework, I''m leaving the actual code to you! :laugh:
你也可以使用LINQ
You can also to it by using LINQ
var str = "abcd123";
foreach (string letter in str.Select(c => Convert.ToString(c, 2)))
{
Console.WriteLine(letter);
}
Console.ReadLine();
更多参考: -
如何在C#中将字符串转换为ascii为二进制文件 [ ^ ]
这篇关于在C#中阅读Bynary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文