I wanna know how it is possible to read a file in binary format.
for example a tiff image file may have the following binary format in hex 0000 4949 002A 0000.
how can i get these values in c#?
Here is how I usually read files in hexadecimal format, changed for the header, as you need:
using System;
using System.Linq;
using System.IO;
namespace FileToHex
{
class Program
{
static void Main(string[] args)
{
//read only 4 bytes from the file
const int HEADER_SIZE = 4;
byte[] bytesFile = new byte[HEADER_SIZE];
using (FileStream fs = File.OpenRead(#"C:\temp\FileToHex\ex.tiff"))
{
fs.Read(bytesFile, 0, HEADER_SIZE);
fs.Close();
}
string hex = BitConverter.ToString(bytesFile);
string[] header = hex.Split(new Char[] { '-' }).ToArray();
Console.WriteLine(System.String.Join("", header));
Console.ReadLine();
}
}
}
You can use the ReadAllBytes method of the System.IO.File class to read the bytes into an array:
System.IO.FileStream fs = new System.IO.FileStream(#"C:\Temp\sample.pdf", System.IO.FileMode.Open, System.IO.FileAccess.Read);
int size = 1024;
byte[] b = new byte[size];
fs.Read(b, 0, size);
I have not used LibTIFF.Net, http://bitmiracle.com/libtiff but it seems to be fairly complete.
Using it, instead of reading the file as bytes and then decoding the header(s) may be a lot easier for you.
Related
One way or another, all digital data is stored in 0 and 1. That's the principle of binary data, I guess.
Is there a method or package that can show you the binary code of a file/single-exe-program of how it is actually being stored in the 0/1 format??
I would see it like:
- import a certain, random file
- convert it to it's 0/1 format
- store the the 1/0-data in a txt (streamwriter/binarywriter)
if yes, is this available in any .NET language (pref: c#)?
Essentially you just need to break this into two steps:
Convert a file into bytes
Convert a byte into a binary string
The first step is easy:
var fileBytes = File.ReadAllBytes(someFileName);
The second step is less straightforward, but still pretty easy:
var byteString = string.Concat(fileBytes.Select(x => Convert.ToString(x, 2).PadLeft(8, '0')))
The idea here is that you select each byte individually, converting each one to a binary string (pad left so each one is 8 characters, since many bytes have leading zeroes), and concatenate all of those into a single string. (Courtesy in part of #xanatos' comment below.)
I think this is something what you are looking for:
byte [] contents = File.ReadAllBytes(filePath);
StringBuilder builder = new StringBuilder();
for(int i = 0; i<contents .Length; i++)
{
builder.Append( Convert.ToString(contents[i], 2).PadLeft(8, '0') );
}
Now, you can for example write builder contents to a text file.
this will stream the conversion, useful if you have huge file.
using System;
using System.IO;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var buffer = new byte[1024];
int pos = 0;
using (var fileIn = new FileStream(#"c:\test.txt", FileMode.Open, FileAccess.Read))
using (var fileOut = new FileStream(#"c:\test.txt.binary", FileMode.Create, FileAccess.Write))
while((pos = fileIn.Read(buffer,0,buffer.Length)) > 0)
foreach (var value in buffer.Take(pos).Select(x => Convert.ToString(x, 2).PadLeft(8, '0')))
fileOut.Write(value.Select(x => (byte)x).ToArray(), 0, 8);
}
}
}
You can open the file in binary mode. Didn't test it but it should work :
BitArray GetBits(string fuleSrc)
{
byte[] bytesFile;
using (FileStream file = new FileStream(fuleSrc, FileMode.Open, FileAccess.Read))
{
bytesFile = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
}
return new BitArray(bytesFile);
}
A solution using FileStream, StreamWriter, StringBuilder and Convert
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
using (FileStream fs = new FileStream(InputFILEPATH, FileMode.Open))
{
while (fs.Position != fs.Length)
{
sb.Append(Convert.ToString(fs.ReadByte(),2));
}
}
using (StreamWriter stw = new StreamWriter(File.Open(OutputFILEPATH,FileMode.OpenOrCreate)))
{
stw.WriteLine(sb.ToString());
}
Console.ReadKey();
}
I want to compress a file that has binary data and save the compressed data in another file:
FileStream fileStream = new FileStream("compressed_file.bin", FileMode.Create, FileAccess.Write);
GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Compress);
StreamWriter writer = new StreamWriter(compressionStream);
writer.Write(File.ReadAllBytes("file_to_be_compressed.bin"), 0, File.ReadAllBytes("file_to_be_compressed.bin").Length);
writer.Close();
I get following error:
cannot convert from 'byte[]' to 'char[]'
in line:
writer.Write(File.ReadAllBytes("file_to_be_compressed.bin"), 0, File.ReadAllBytes("file_to_be_compressed.bin").Length)
And is it fine to convert the binary data of file to byte array, or is it better to pass binary data of file as stream?
Note: CopyTo is not available in .NET 2.0
Try this, according to http://www.dotnetperls.com/gzipstream
using System.IO;
using System.IO.Compression;
using System.Text;
class Program
{
static void Main()
{
try
{
// 1.
// Starting file is 26,747 bytes.
string anyString = File.ReadAllText("TextFile1.txt");
// 2.
// Output file is 7,388 bytes.
CompressStringToFile("new.gz", anyString);
}
catch
{
// Could not compress.
}
}
public static void CompressStringToFile(string fileName, string value)
{
// A.
// Write string to temporary file.
string temp = Path.GetTempFileName();
File.WriteAllText(temp, value);
// B.
// Read file into byte array buffer.
byte[] b;
using (FileStream f = new FileStream(temp, FileMode.Open))
{
b = new byte[f.Length];
f.Read(b, 0, (int)f.Length);
}
// C.
// Use GZipStream to write compressed bytes to target file.
using (FileStream f2 = new FileStream(fileName, FileMode.Create))
using (GZipStream gz = new GZipStream(f2, CompressionMode.Compress, false))
{
gz.Write(b, 0, b.Length);
}
}
}
H,
How to Read First 512 Bytes of data from a .dat file in C# ?
My dat files contains binary data.
I am using File.ReadAllBytes currently to read data from the dat file.But it Reads all the data, i want to read only first 512 bytes then break.
I need to use for loop for this or any other approach.
Any help is appreciated.
You can try this:
byte[] buffer = new byte[512];
try
{
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
var bytes_read = fs.Read(buffer, 0, buffer.Length);
fs.Close();
if (bytes_read != buffer.Length)
{
// Couldn't read 512 bytes
}
}
}
catch (System.UnauthorizedAccessException ex)
{
Debug.Print(ex.Message);
}
You can use a byte[] variable, and FileStream.Read for that.
A simple but effective approach:
var result = ""; //Define a string variable. This doesn't have to be a string, this is just an example.
using (BinaryReader br = new BinaryReader(File.OpenRead(openFileDailog1.FileName))) //Begin reading the file with the BinaryReader class.
{
br.BaseStream.Seek(0x4D, SeekOrigin.Begin); //Put the beginning of a .dat file here. I put 0x4D, because it's the generic start of a file.
result = Encoding.UTF8.GetString(br.ReadBytes(512)); //You don't have to return it as a string, this is just an example.
}
br.Close(); //Close the BinaryReader.
using System.IO; enables access to the BinaryReader class.
Hope this helps!
I'm currently developing an application in C# that uses Amazon SQS
The size limit for a message is 8kb.
I have a method that is something like:
public void QueueMessage(string message)
Within this method, I'd like to first of all, compress the message (most messages are passed in as json, so are already fairly small)
If the compressed string is still larger than 8kb, I'll store it in S3.
My question is:
How can I easily test the size of a string, and what's the best way to compress it?
I'm not looking for massive reductions in size, just something nice and easy - and easy to decompress the other end.
To know the "size" (in kb) of a string we need to know the encoding. If we assume UTF8, then it is (not including BOM etc) like below (but swap the encoding if it isn't UTF8):
int len = Encoding.UTF8.GetByteCount(longString);
Re packing it; I would suggest GZIP via UTF8, optionally followed by base-64 if it has to be a string:
using (MemoryStream ms = new MemoryStream())
{
using (GZipStream gzip = new GZipStream(ms, CompressionMode.Compress, true))
{
byte[] raw = Encoding.UTF8.GetBytes(longString);
gzip.Write(raw, 0, raw.Length);
gzip.Close();
}
byte[] zipped = ms.ToArray(); // as a BLOB
string base64 = Convert.ToBase64String(zipped); // as a string
// store zipped or base64
}
Give unzip bytes to this function.The best I could come up with was
public static byte[] ZipToUnzipBytes(byte[] bytesContext)
{
byte[] arrUnZipFile = null;
if (bytesContext.Length > 100)
{
using (var inFile = new MemoryStream(bytesContext))
{
using (var decompress = new GZipStream(inFile, CompressionMode.Decompress, false))
{
byte[] bufferWrite = new byte[4];
inFile.Position = (int)inFile.Length - 4;
inFile.Read(bufferWrite, 0, 4);
inFile.Position = 0;
arrUnZipFile = new byte[BitConverter.ToInt32(bufferWrite, 0) + 100];
decompress.Read(arrUnZipFile, 0, arrUnZipFile.Length);
}
}
}
return arrUnZipFile;
}
I'm needing to create a zip file containing documents that exist on the server. I am using the .Net Package class to do so, and to create a new Package (which is the zip file) I have to have either a path to a physical file or a stream. I am trying to not create an actual file that would be the zip file, instead just create a stream that would exist in memory or something.
My question is how do you instantiate a new Stream (i.e. FileStream, MemoryStream, etc) without having a physical file to instantiate from.
MemoryStream has several constructor overloads, none of which require a file.
There is an example of how to do this on the MSDN page for MemoryStream:
using System;
using System.IO;
using System.Text;
class MemStream
{
static void Main()
{
int count;
byte[] byteArray;
char[] charArray;
UnicodeEncoding uniEncoding = new UnicodeEncoding();
// Create the data to write to the stream.
byte[] firstString = uniEncoding.GetBytes(
"Invalid file path characters are: ");
byte[] secondString = uniEncoding.GetBytes(
Path.GetInvalidPathChars());
using(MemoryStream memStream = new MemoryStream(100))
{
// Write the first string to the stream.
memStream.Write(firstString, 0 , firstString.Length);
// Write the second string to the stream, byte by byte.
count = 0;
while(count < secondString.Length)
{
memStream.WriteByte(secondString[count++]);
}
// Write the stream properties to the console.
Console.WriteLine(
"Capacity = {0}, Length = {1}, Position = {2}\n",
memStream.Capacity.ToString(),
memStream.Length.ToString(),
memStream.Position.ToString());
// Set the position to the beginning of the stream.
memStream.Seek(0, SeekOrigin.Begin);
// Read the first 20 bytes from the stream.
byteArray = new byte[memStream.Length];
count = memStream.Read(byteArray, 0, 20);
// Read the remaining bytes, byte by byte.
while(count < memStream.Length)
{
byteArray[count++] =
Convert.ToByte(memStream.ReadByte());
}
// Decode the byte array into a char array
// and write it to the console.
charArray = new char[uniEncoding.GetCharCount(
byteArray, 0, count)];
uniEncoding.GetDecoder().GetChars(
byteArray, 0, count, charArray, 0);
Console.WriteLine(charArray);
}
}
}
Is this what you are looking for?
You can create a new stream and write to it. You don't need a file to construct the object.
http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx
Write Method:
http://msdn.microsoft.com/en-us/library/system.io.memorystream.write.aspx
Constructors for Memory Stream:
http://msdn.microsoft.com/en-us/library/system.io.memorystream.memorystream.aspx