You can use the BinaryReader class to handle this. Alternatively, you can just read in a FileStream and use a BitArray to store the array of bit values :
using (FileStream stream = new FileStream("yourFilePath", FileMode.Open))
{
int size = (int)stream.Length;
byte[] data = new byte[size];
stream.Read(data, 0, size);
//Stores the bit values of the file
BitArray bitArray = new BitArray(data);
}
You may be able to use the above (BitArray method) with the following to get a string containing your bit values :
public static string ToBitString(BitArray bits)
{
var sb = new StringBuilder();
for (int i = 0; i < bits.Count; i++)
{
char c = bits[i] ? '1' : '0';
sb.Append(c);
}
return sb.ToString();
}
all modern computers (excluding experimental computers) are binary in nature ...
every byte is made up of binary digits (bits) that have one of two arithmetical values, either
zero or one.
netraswable
i have docx,odc,pdf,zip,mp3,ppt etc files i want to convert them into their equivalent binary code.
netraswable, they are already in binary code ... what makes their contents meaningful are the computer programs that interpret their bits and bytes into something useful to
a human; for example, Windows Media Player can turn a .mp3 final into sound and Microsoft PowerPoint can show us a slide show from a .ppt file ... OTOH, Microsoft Word would be totally lost with a .mp3 file.
netraswable, please clarify your question; if you tell us what you want to achieve and why, that would help; otherwise, your peers here at forums.asp.net will either ignore
your question or answer some question that you have not even asked.
Member
Rion Williams may have understood your question better than myself ... please let us know if his reply answers your question.
Also, a .zip file generally contains one or more files that are compressed, so reading them bit by bit is of little use.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Marked as answer by Mark - MSFT on Feb 04, 2013 04:34 AM
netraswable
Member
31 Points
48 Posts
Convert any type of file in binarycode.
Jan 26, 2013 02:48 PM|LINK
Hello All,
I have docx,odc,pdf,zip,mp3,ppt etc files i want to convert them into their equivalent binary code.
Thanks in Advance.
Rion William...
All-Star
31832 Points
5185 Posts
Re: Convert any type of file in binarycode.
Jan 26, 2013 02:52 PM|LINK
You can use the BinaryReader class to handle this. Alternatively, you can just read in a FileStream and use a BitArray to store the array of bit values :
using (FileStream stream = new FileStream("yourFilePath", FileMode.Open)) { int size = (int)stream.Length; byte[] data = new byte[size]; stream.Read(data, 0, size); //Stores the bit values of the file BitArray bitArray = new BitArray(data); }You may be able to use the above (BitArray method) with the following to get a string containing your bit values :
public static string ToBitString(BitArray bits) { var sb = new StringBuilder(); for (int i = 0; i < bits.Count; i++) { char c = bits[i] ? '1' : '0'; sb.Append(c); } return sb.ToString(); }gerrylowry
All-Star
20577 Points
5721 Posts
Re: Convert any type of file in binarycode.
Jan 26, 2013 04:18 PM|LINK
@ netraswable welcome to forums.asp.net
sorry, i really do not understand what you are actually asking, i hope you will clarify your question.
FWIW, imho, this article is worth your time to read it (i'm biased because i wrote it):
http://weblogs.asp.net/gerrylowry/archive/2012/11/10/clarity-is-important-both-in-question-and-in-answer.aspx
more information
all modern computers (excluding experimental computers) are binary in nature ...
every byte is made up of binary digits (bits) that have one of two arithmetical values, either zero or one.
netraswable, they are already in binary code ... what makes their contents meaningful are the computer programs that interpret their bits and bytes into something useful to a human; for example, Windows Media Player can turn a .mp3 final into sound and Microsoft PowerPoint can show us a slide show from a .ppt file ... OTOH, Microsoft Word would be totally lost with a .mp3 file.
netraswable, please clarify your question; if you tell us what you want to achieve and why, that would help; otherwise, your peers here at forums.asp.net will either ignore your question or answer some question that you have not even asked.
Member Rion Williams may have understood your question better than myself ... please let us know if his reply answers your question.
Also, a .zip file generally contains one or more files that are compressed, so reading them bit by bit is of little use.
g.