How Convert HEX To JPG File In C#

Posted on 2013-08-27
Medium Priority
Last Modified: 2013-08-27
Hi Expert,

Now I need to know that how we can convert from HEX to JPG File in C# Code.

Thanks in advance
Question by:Asrar Azmi
  • 3
  • 2
LVL 62

Expert Comment

by:Julian Hansen
ID: 39441402
Might I offer a suggestion.

Your use of JPG in your questions is confusing. For what you are doing a JPG file is just a file of binary data.

Your question should be (should have been)

How can I output a binary file hex values  i.e.

0x0A 0x15 0xFE ...

Your question now should read

How do I read a file of hex values and create the corresponding binary file i.e.

I have
0x0A 0x15 0xFE ...

And I want to output the binary equivalents of these values.

I think you will find your answer a lot quicker.

Author Comment

by:Asrar Azmi
ID: 39441455
Dear Expert ,
Pls reply me that what I want just send me the code to convert hex to jpg file in c#  as Mr. hjgode replied me before .

See the link below
His Reply.
LVL 62

Accepted Solution

Julian Hansen earned 2000 total points
ID: 39441754
Add to the class from your previous question
        public static void doHex2Jpeg(string sHexFileName)
            string file_in = sHexFileName;	//the input file name
            string file_out = file_in + ".jpg"; //the output

            FileStream fs = new FileStream(file_out, FileMode.CreateNew);
            BinaryWriter bw = new BinaryWriter(fs, new ASCIIEncoding());
            byte[] chunk = System.IO.File.ReadAllBytes(file_in);
            for(int i=0; i< chunk.Length;i+=2) {
                byte val = (byte)(((chunk[i] - 87 < 0) ? chunk[i] - 48 : chunk[i] - 87) *16 + 
                    ((chunk[i + 1] - 87 < 0) ? chunk[i + 1] - 48 : chunk[i + 1] - 87));

Open in new window

Call like this

Open in new window

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

LVL 11

Expert Comment

ID: 39441799
hi.. can you try this code for conversion

private void button1_Click(object sender, EventArgs e)
            // Some junk hex image data
string hexImgData = @"FFD8FFE000104A46494600010200006400640000FFFFD9";
        // Call function to Convert the hex data to byte array
            byte[] newByte = ToByteArray(hexImgData);
            MemoryStream memStream = new MemoryStream(newByte);

// Save the memorystream to file

    // Function converts hex data into byte array
   public static byte[] ToByteArray(String HexString)
            int NumberChars = HexString.Length;

            byte[] bytes = new byte[NumberChars / 2];

            for (int i = 0; i < NumberChars; i += 2)
                bytes[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);
            return bytes;

Author Closing Comment

by:Asrar Azmi
ID: 39441810
Thanks alot this is I needed.
LVL 62

Expert Comment

by:Julian Hansen
ID: 39442392
You are welcome.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The PowerShell Core 6.0 of .NET release is just the beginning. The upcoming PowerShell Core 6.1 would have artificial intelligence and internet of things capabilities. So many things to look forward to in the upcoming release.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Through the video, you can check the migration process of Outlook PST file to PDF. Kernel for Outlook to PDF tool can convert Outlook emails with all attributes like Subject, To, From, Cc, Bcc and other folders such as Inbox, Outbox, Sent Items, Jun…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question