0

I have a problem about converting binary to hex then hex to binary.
For example I have an image file. First I want to convert that image to a hex string, then I want to insert this hex string to database. When a user wants to get that image file, the program has to read the hex string from the databasae, then converts to an image file. Is it possible in C# ?

I have tried some sample methods from StackOverflow but I can't do that. I did an image file but it can't be shown.

I am waiting for your help.
Thank you

omfar
  • 1
  • 1
  • 1
  • 10
    You should store the raw `byte[]` in the database – SLaks Jul 27 '12 at 12:56
  • I agree with SLaks, check this out to see, how you write a byte array to a sql server database: http://stackoverflow.com/questions/1064121/how-do-i-insert-a-byte-into-an-sql-server-varbinary-column – Hinek Jul 27 '12 at 13:02
  • You should try Base64 conversion as its unicode based..Check this out here http://stackoverflow.com/questions/7240216/convert-an-image-to-base64-and-vice-versa – Mayank Pathak Jul 27 '12 at 13:03
  • Remember to keep in mind that if this is a high-traffic process you'll be storing a lot of objects on the large object heap. – Simon Whitehead Jul 27 '12 at 13:12

2 Answers2

1

Let us know if this helps you out -

    private string GetHexStringFromImage(System.Drawing.Image imageToConvert)
    {
        //Convert image it to byte-array
        byte[] byteArray;
        using (MemoryStream ms = new MemoryStream())
        {
            imageToConvert.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            byteArray = ms.ToArray();
        }

        //Convert byte-array to Hex-string
        StringBuilder hexBuilder = new StringBuilder();
        foreach (byte b in byteArray)
        {
            string hexByte = b.ToString("X");

            //make sure each byte is represented by 2 Hex digits
            string tempString = hexByte.Length % 2 == 0 ? hexByte : hexByte.PadLeft(2, '0');

            hexBuilder.Append(tempString);
        }

        //return Hex-string to save to DB
        return hexBuilder.ToString();
    }

    private System.Drawing.Image GetImageFromHexString(string hexSting)
    {
        //Convert Hex-string from DB to byte-array
        int length = hexSting.Length;
        List<byte> byteList = new List<byte>();

        //Take 2 Hex digits at a time
        for (int i = 0; i < length; i += 2)
        {
            byte byteFromHex = Convert.ToByte(hexSting.Substring(i, 2), 16);
            byteList.Add(byteFromHex);
        }
        byte[] byteArray = byteList.ToArray();

        //Convert byte-array to image file and return the image
        using (MemoryStream stream = new MemoryStream(byteArray))
        {
            return System.Drawing.Image.FromStream(stream);
        }
    }
atiyar
  • 7,762
  • 6
  • 34
  • 75
0

You can use the JSON.NET library to serialize the byte[] into a string which will turn it into hex. Then use the same library to deserialize it back a byte[] when you need to.

I would recommend that you store it as a byte[] in the database though because it will use at least twice the space as a string (one byte in hex is 2 characters, each being a byte at minimum with ASCII or UTF8) and there is a tiny bit of overhead for the JSON format.

BlargleMonster
  • 1,602
  • 2
  • 18
  • 33