How to Save a Binary Representation to file - c#

I have the following textual binary representation: "0x255044462D312E340D0A25FFFFFFF..."
I know it's a pdf.
I know it's the textual represantation from a sql server column (image data type).
But im lost to find out how to save this binary to a pdf file on my disk and view the content.
Maybe someone can hint me in the right direction.
Best Regards and Thanks in Advance

You're correct that it is a PDF file (at least it masquerades like on. You have hexadecimally encoded bytes; the first read:
255044462D312E340D0A
%PDF-1.4<CR><LF>
So you appear to have a PDF 1.4 string.
Just take two characters from the string, treat them as hex, convert them to the correct byte and write them to a file. Write binary, not textually (you don't want to add additional line-breaks in there, PDF is too binary to let that work.
(I did the conversion using this site: http://www.dolcevie.com/js/converter.html)

I'm not sure what database you are working with or how you are getting your string that you have above.
Many databases allow you to save binary data as a blob or some other byte array type. I believe in MSSQL this is called an "image" but I am not 100% on that. I would start by looking into the two following links in order. The first link talks about how to pull byte array data from a database. The example is in Visual Basic but should be easily changed to C# if that is what you are using.
The second link contains an example of how to save that byte array data to the file system.
I would also suggest posting some of the code you have tried as well so that the community may comment and point out areas you possibly had misunderstandings on.
1.) http://support.microsoft.com/kb/308042
2.) Save and load MemoryStream to/from a file

http://www.pdfsharp.com/PDFsharp/ can read in binary data and you can call .Save() and it will make the PDF file to disk for you.

Related

How to encode and decode a text file to binary in c#?

For my Unity Game, I want to save player data by using text files. Text files can be easily modified and so can the data in them be modified. So, I would like to convert the text files to 0's and 1's. So that when you open it you should see 0101011 instead of readable and editable data. I know that I can use Read Bytes of File and replace the text in the file with this data, but how in the world do I make it data again? I need help with that.
Text files can be easily modified and so can the data in them be modified.
Is this a problem? games are made to have fun, so why do you want to stop the user from gaming how he wants? If it is a competitive multiplayer game, or any type of game involving actual money, you should not rely on any data on the client.
So, I would like to convert the text files to 0's and 1's. So that when you open it you should see 0101011 instead of readable and editable data
Please do not do this, it will just reduce performance for no benefit. If you do not want trivial modification of data, just use a binary serialization format. Or use the Unity provided storage options. Using binary serialization will likely be faster and produce smaller files, at the cost of making modifications more difficult. But you do not seem concerned about the last point.
Converting objects to serialized data and back again is a common operation, and there are great libraries for it. Json is common for textual data, I have used Protobuf .net for binary data, and find it quite fast and easy to use.

Convert word/excel bytes to pdf document and show to ASP.NET MVC view

I have been searching for a way to convert word/excel byte array from sql server to a pdf file and then I want to show it to the view using <object> tag.
But I cant find any proper solution to this problem online because almost of them use non-free packages etc.
Does anybody know a way how to do it in c# without using other libraries ?
If there is no way to do this please recommend me any package that is free and simple because I just want to use it for this purpose.
Refering to this question here I could use https://docs.google.com/gview?url=myDocUrl or https://view.officeapps.live.com/op/embed.aspx?src= but I dont know how to make them work with files saved in database as byte arrays and how to test them locally in visual studio.
If anybody knows how to use them with byte arrays please let me know as well.
Thanks in advance.
First, you'll need to put your byte array into a format that the Office API can understand--probably save the byte array to a file on disk, then using the Word or Excel API library (Microsoft.Office namespace) to open the file and process it (such as saving it to a PDF file).

Spire.PDF Load Binary PDF Data into PdfDocument

I'm working in C# with Spire.PDF. Specifically, my goal is to load binary PDF data from a database into a Spire.Pdf.PdfDocument object.
According to this documentation, I should be able to use the LoadFromStream() method or some unspecified method that takes a byte array (see bottom of page at link where there is a link but only to the general documentation). However, this method seems absent from the current NuGet package.
So, in summary, how can I make a Spire.Pdf.PdfDocument object using a byte array of data? Thanks in advance.
Please comment if you know a better .NET library for converting PDFs to and from images.

images added into resx file

This has been a interesting file for me.
I found that ,whenever a image is added to this ,the image is actually converted into bytes and stored as data rather than a file.
I just want to know the benefit of this?also will be there be chances of bytes getting curropted when the file is put into version control(Most unlikely)
A file is nothing else but data written to disk and usually contains the actual data plus some header information (e.g. image type, so you know how to interprete the data). If it helps you understand it, think of the resx file as a "mini" file system from which the image data can be retrieved.
If put in version control, resx files should be no problem.
You might want to take a look at ResourceManager.

Is there an easy way to determine the type of a file without knowing the file's extension?

I have a table with a binary column which stores files of a number of different possible filetypes (PDF, BMP, JPEG, WAV, MP3, DOC, MPEG, AVI etc.), but no columns that store either the name or the type of the original file. Is there any easy way for me to process these rows and determine the type of each file stored in the binary column? Preferably it would be a utility that only reads the file headers, so that I don't have to fully extract each file to determine its type.
Clarification: I know that the approach here involves reading just the beginning of each file. I'm looking for a good resource (aka links) that can do this for me without too much fuss. Thanks.
Also, just C#/.NET on Windows, please. I'm not using Linux and can't use Cygwin (doesn't work on Windows CE, among other reasons).
you can use these tools to find the file format.
File Analyser
http://www.softpedia.com/get/Programming/Other-Programming-Files/File-Analyzer.shtml
What Format
http://www.jozy.nl/whatfmt.html
PE file format analyser
http://peid.has.it/
This website may be helpful for you.
http://mark0.net/onlinetrid.aspx
Note:
i have included the download links to make sure that you are getting the right tool name and information.
please verify the source before you download them.
i have used a tool in the past i think it is File Analyser, which will tell you the closest match.
happy tooling.
This is not a complete answer, but a place to start would be a "magic numbers" library. This examines the first few bytes of a file to determine a "magic number", which is compared against a known list of them. This is (at least part) of how the file command on Linux systems works.
Someone else asked a similar question and posted the code used to do exactly this. You should be able to take what is posted here, and slightly modify it so that it pulls from your database.
https://stackoverflow.com/questions/58510
In addition to that, it looks like someone has written a library based off of magic numbers to do this, however, it looks like the site requires registration, and some form of alternate access in order to download this lirbary. The documentation is avaliable for free without registration, that may be helpful.
http://software.topcoder.com/catalog/c_component.jsp?comp=13249160&ver=2
The easiest way I know is to use file command that it is also available in Windows with Cygwin .
A lot of filetypes have well defined headers that begin the file. You could check the first few bytes to check to see how the file begins.
Easiest way to do this would be through access to a *nix (or cygwin) system that has the 'file' command:
$ file visitors.*
visitors.html: HTML document text
visitors.png: PNG image data, 5360 x 2819, 8-bit colormap, non-interlaced
You could write a C# application that piped the first X bytes of each binary column to the file command (using - as the file name)
You need to use some p/invoke interop code to call the SHGetFileInfo method from the Win32 API. This article may also help.

Categories