converting file to binary c# - c#

I want to convert file into binary. I tried, but I get 0X000000000000000..... It is not correct. Always every file getting that digits. Please help me to solve thanks in advance
if (value.resume_file.CompareTo("") != 0)
{
byte[] binary = new byte[value.resume_file.Length];
//binary = Convert.ToByte(value.resume_file);
objJobSeekers.IsResume = true;
objJobSeekers.DocFileName = value.resume_file;
objJobSeekers.Resume = binary;
objJobSeekers.TypedResume = DBNull.Value;
}

string to byte[]
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);
byte[] to string
str = System.Text.Encoding.UTF8.GetString(bytes);
Is this what you looking for?

If you want to read any file from disk and get its bytes then use
string FileDir = "D:\\File.doc";
byte[] MyBytes = File.ReadAllBytes(FileDir);
If you want to convert some random object in memory into bytes then you can use a BinarySerializer
byte[] MyBytes;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, obj);
MyBytes = ms.ToArray();
}

Related

what wrong with my binary compression with C#

I try to compress some binary string with deflate32 but something is going wrong.
string myString = "101111110101010111111111111";
// convert string to stream
byte[] byteArray = Encoding.UTF8.GetBytes(myString);
MemoryStream stream = new MemoryStream(byteArray);
var compressed = new DeflateStream(stream, CompressionLevel.Fastest);
Console.WriteLine(compressed); // Can't see any compressed string
Where is my mistake?

How to convert UTF16LE to string C#?

I have a file with text in UTF16LE like so
feff 0074 0065 0073 0074
which is basically
test
What I am trying to is to read that file:
using (FileStream stream = File.Open(fileName, FileMode.Open))
{
byte[] b = new byte[stream.Length];
Encoding u16LE = Encoding.Unicode;
arrRaw.Add(u16LE.GetString(b));
}
After that im starting reading those string array in another function:
[![enter image description here][2]][2]
And so the output of that is just hieroglyphs. I don't know what I'm doing wrong. Moreover I was trying to start reading from the third byte in that array, but the result was the same
public static string Utf16ToUtf8Initial(string utf16String)
{
string utf8String = String.Empty;
byte[] utf16Bytes = Encoding.Unicode.GetBytes(utf16String);
byte[] utf8Bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, utf16Bytes);
utf8String = Encoding.UTF8.GetString(utf8Bytes);
return utf8String;
}

Decode Base64 and Inflate Zlib compressed XML

Sorry for the long post, will try to make this as short as possible.
I'm consuming a json API (which has zero documentation of course) which returns something like this:
{
uncompressedlength: 743637,
compressedlength: 234532,
compresseddata: "lkhfdsbjhfgdsfgjhsgfjgsdkjhfgj"
}
The data (xml in this case) is compressed and then base64 encoded data which I am attempting to extract. All I have is their demo code written in perl to decode it:
use Compress::Zlib qw(uncompress);
use MIME::Base64 qw(decode_base64);
my $uncompresseddata = uncompress(decode_base64($compresseddata));
Seems simple enough.
I've tried a number of methods to decode the base64:
private string DecodeFromBase64(string encodedData)
{
byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
string returnValue = System.Text.Encoding.Unicode.GetString(encodedDataAsBytes);
return returnValue;
}
public string base64Decode(string data)
{
try
{
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(data);
int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
string result = new String(decoded_char);
return result;
}
catch (Exception e)
{
throw new Exception("Error in base64Decode" + e.Message);
}
}
And I have tried using Ionic.Zip.dll (DotNetZip?) and zlib.net to inflate the Zlib compression. But everything errors out. I am trying to track down where the problem is coming from. Is it the base64 decode or the Inflate?
I always get an error when inflating using zlib: I get a bad Magic Number error using zlib.net and I get "Bad state (invalid stored block lengths)" when using DotNetZip:
string decoded = DecodeFromBase64(compresseddata);
string decompressed = UnZipStr(GetBytes(decoded));
public static string UnZipStr(byte[] input)
{
using (MemoryStream inputStream = new MemoryStream(input))
{
using (Ionic.Zlib.DeflateStream zip =
new Ionic.Zlib.DeflateStream(inputStream, Ionic.Zlib.CompressionMode.Decompress))
{
using (StreamReader reader =
new StreamReader(zip, System.Text.Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
}
}
After reading this:
http://george.chiramattel.com/blog/2007/09/deflatestream-block-length-does-not-match.html
And listening to one of the comments. I changed the code to this:
MemoryStream memStream = new MemoryStream(Convert.FromBase64String(compresseddata));
memStream.ReadByte();
memStream.ReadByte();
DeflateStream deflate = new DeflateStream(memStream, CompressionMode.Decompress);
string doc = new StreamReader(deflate, System.Text.Encoding.UTF8).ReadToEnd();
And it's working fine.
This was the culprit:
http://george.chiramattel.com/blog/2007/09/deflatestream-block-length-does-not-match.html
With skipping the first two bytes I was able to simplify it to:
MemoryStream memStream = new MemoryStream(Convert.FromBase64String(compresseddata));
memStream.ReadByte();
memStream.ReadByte();
DeflateStream deflate = new DeflateStream(memStream, CompressionMode.Decompress);
string doc = new StreamReader(deflate, System.Text.Encoding.UTF8).ReadToEnd();
First, use System.IO.Compression.DeflateStream to re-inflate the data. You should be able to use a MemoryStream as the input stream. You can create a MemoryStream using the byte[] result of Convert.FromBase64String.
You are likely causing all kinds of trouble trying to convert the base64 result to a given encoding; use the raw data directly to Deflate.

How to Read an embedded resource as array of bytes without writing it to disk?

In my application I compile another program from source.cs file using CodeDom.Compiler and I embed some resources ( exe and dll files ) at compile time using :
// .... rest of code
if (provider.Supports(GeneratorSupport.Resources))
{
cp.EmbeddedResources.Add("MyFile.exe");
}
if (provider.Supports(GeneratorSupport.Resources))
{
cp.EmbeddedResources.Add("New.dll");
}
// ....rest of code
In the compiled file, I need to read the embedded resources as array of bytes. Now I'm doing that by extracting the resources to disk using the function below and the use
File.ReadAllBytes("extractedfile.exe");
File.ReadAllBytes("extracteddll.dll");
I do this after extracting the two files to disk using this function :
public static void ExtractSaveResource(String filename, String location)
{
// Assembly assembly = Assembly.GetExecutingAssembly();
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
// Stream stream = assembly.GetManifestResourceStream("Installer.Properties.mydll.dll"); // or whatever
// string my_namespace = a.GetName().Name.ToString();
Stream resFilestream = a.GetManifestResourceStream(filename);
if (resFilestream != null)
{
BinaryReader br = new BinaryReader(resFilestream);
FileStream fs = new FileStream(location, FileMode.Create); // say
BinaryWriter bw = new BinaryWriter(fs);
byte[] ba = new byte[resFilestream.Length];
resFilestream.Read(ba, 0, ba.Length);
bw.Write(ba);
br.Close();
bw.Close();
resFilestream.Close();
}
// this.Close();
}
How can I do the same thing (Get the embedded resources as array of bytes) but without writing anything to hard disk?
You are actually already reading the stream to a byte array, why not just stop there?
public static byte[] ExtractResource(String filename)
{
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
using (Stream resFilestream = a.GetManifestResourceStream(filename))
{
if (resFilestream == null) return null;
byte[] ba = new byte[resFilestream.Length];
resFilestream.Read(ba, 0, ba.Length);
return ba;
}
}
edit: See comments for a preferable reading pattern.
Simple alternative using a MemoryStream:
var ms = new MemoryStream();
await resFilestream.CopyToAsync(ms);
var bytes = ms.ToArray();
Keep in mind that Embedded resource filename = Assemblyname.fileName
string fileName = "test.pdf";
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
string fileName = a.GetName().Name + "." + "test.pdf";
using (Stream resFilestream = a.GetManifestResourceStream(fileName))
{
if (resFilestream == null) return null;
byte[] ba = new byte[resFilestream.Length];
resFilestream.Read(ba, 0, ba.Length);
var byteArray = ba;
}
if you are reading an embeded resource here is a simple way to do so.
string resourcePath = "pack://application:,,,/resource/location/S_2/{0}";
StreamResourceInfo resInfo = Application.GetResourceStream(new Uri(resourcePath));
if (resInfo == null)
{
throw new Exception("Resource not found: " + resourcePath);
}
var ms = new System.IO.MemoryStream();
await resInfo.Stream.CopyToAsync(ms);
byte[] bytes = ms.ToArray();
File.WriteAllBytes(#"C:\Users\admin\Desktop\MyFile.exe", Resources.BinFile); // binary file
File.WriteAllText(#"C:\Users\admin\Desktop\text.txt", Resources.TextFile); // text file

C# Object Binary Serialization

I want to make a binary serialize of an object and the result to save it in a database.
Person person = new Person();
person.Name = "something";
MemoryStream memorystream = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(memorystream, person);
How can I transform memorystream in a string type to be saved in database, and after this to be able to deserialize the object?
What you're really asking for is a safe way of representing arbitrary binary data as text and then converting it back again. The fact that it stores a serialized object is irrelevant.
The answer is almost to use Base 64 (e.g. Convert.ToBase64String and Convert.FromBase64String). Do not use Encoding.UTF8.GetString or anything similar - your binary data is not encoded text data, and shouldn't be treated as such.
However, does your database not have a data type for binary data? Check for BLOB, IMAGE and BINARY types...
Here's the sample. TData must be marked [Serializable] and all fields type also.
private static TData DeserializeFromString<TData>(string settings)
{
byte[] b = Convert.FromBase64String(settings);
using (var stream = new MemoryStream(b))
{
var formatter = new BinaryFormatter();
stream.Seek(0, SeekOrigin.Begin);
return (TData)formatter.Deserialize(stream);
}
}
private static string SerializeToString<TData>(TData settings)
{
using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, settings);
stream.Flush();
stream.Position = 0;
return Convert.ToBase64String(stream.ToArray());
}
}
//-------write to database-------------------------
Person person = new Person();
person.name = "Firstnm Lastnm";
MemoryStream memorystream = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(memorystream, person);
byte[] yourBytesToDb = memorystream.ToArray();
//here you write yourBytesToDb to database
//----------read from database---------------------
//here you read from database binary data into yourBytesFromDb
MemoryStream memorystreamd = new MemoryStream(yourBytesFromDb);
BinaryFormatter bfd = new BinaryFormatter();
Person deserializedperson = bfd.Deserialize(memorystreamd) as Person;
I used something like this
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, Person);
memoryStream.Flush();
memoryStream.Position = 0;
string value = Convert.ToBase64String(memoryStream.ToArray());
Basically, don't save the data as string to the database, there are blob fields available to store binary data.
If you really need to have the data as string, you'll need to convert your byte[] to a string using base64 encoding, and to grab the byte[] from a string use decoding.
Have you not looked into converting the memorystream into a base64hex string to be put into the database?
byte[] mStream = memorystream.ToArray();
string sConvertdHex = System.Convert.ToBase64String(mStream)
Then you can dump the contents sConvertdHex to the database. To deserialize it you need to do the reverse
byte[] mData = System.Convert.FromBase64String(...)
then deserialize mData back to your object.

Categories