How Convert HEX To JPG File In C#

Hi Expert,

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

Thanks in advance
Asrar AzmiWeb DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
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.
Asrar AzmiWeb DeveloperAuthor Commented:
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.
Julian HansenCommented:
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
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;
Asrar AzmiWeb DeveloperAuthor Commented:
Thanks alot this is I needed.
Julian HansenCommented:
You are welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.