Convert an Image, encoded in hex, back to an image file
-
Hi all, I have a image that is converted into a hex string (note see below). I would like to convert this hex string back into the image. How can I start with this? Can someone point me out in accomplishing this? Thank you very much. Here under you can find 'a part of' the hex string.
FFD8FFE000104A46494600010100000100010000FFFE002A496E74...
Edit, i removed the biggest part of the hex string because i doesn't look right in this forum. But you can get an idea how it looks like. -
Hi all, I have a image that is converted into a hex string (note see below). I would like to convert this hex string back into the image. How can I start with this? Can someone point me out in accomplishing this? Thank you very much. Here under you can find 'a part of' the hex string.
FFD8FFE000104A46494600010100000100010000FFFE002A496E74...
Edit, i removed the biggest part of the hex string because i doesn't look right in this forum. But you can get an idea how it looks like.I think i have found it. Searching hard and trying end up with nothing, asking help and the answers suddenly comes. O well, here i'll give you the solution. (Remind that the hex value is just a part of whole string)
Private Sub ConvertImg() Dim hexvalue As String = "FFD8FFE000104A46494600010101000100010000..." Dim bytevalue As Byte() = HexToBin(hexvalue) Dim myFileStream As FileStream Dim intByte As Integer = 0 Dim lngLoop As Long Try intByte = bytevalue.Length myFileStream = File.OpenWrite("c:\image.jpg") For lngLoop = 0 To intByte - 1 myFileStream.WriteByte(bytevalue(lngLoop)) Next myFileStream.Close() Catch ex As IOException MessageBox.Show(ex.Message) End Try End Sub '--------------------------------------------------------------------------- ' ' Function: HexToBin() ' Input: s ' Return: bytes ' Purpose: Converts Hex to binary ' '--------------------------------------------------------------------------- Public Shared Function HexToBin(ByVal s As String) As Byte() Dim arraySize As Integer = CInt(s.Length / 2) Dim bytes(arraySize - 1) As Byte Dim counter As Integer For i As Integer = 0 To s.Length - 1 Step 2 Dim hexValue As String = s.Substring(i, 2) ' Tell convert to interpret the string as a 16 bit hex value Dim intValue As Integer = Convert.ToInt32(hexValue, 16) ' Convert the integer to a byte and store it in the array bytes(counter) = Convert.ToByte(intValue) counter += 1 Next Return bytes End Function
-
I think i have found it. Searching hard and trying end up with nothing, asking help and the answers suddenly comes. O well, here i'll give you the solution. (Remind that the hex value is just a part of whole string)
Private Sub ConvertImg() Dim hexvalue As String = "FFD8FFE000104A46494600010101000100010000..." Dim bytevalue As Byte() = HexToBin(hexvalue) Dim myFileStream As FileStream Dim intByte As Integer = 0 Dim lngLoop As Long Try intByte = bytevalue.Length myFileStream = File.OpenWrite("c:\image.jpg") For lngLoop = 0 To intByte - 1 myFileStream.WriteByte(bytevalue(lngLoop)) Next myFileStream.Close() Catch ex As IOException MessageBox.Show(ex.Message) End Try End Sub '--------------------------------------------------------------------------- ' ' Function: HexToBin() ' Input: s ' Return: bytes ' Purpose: Converts Hex to binary ' '--------------------------------------------------------------------------- Public Shared Function HexToBin(ByVal s As String) As Byte() Dim arraySize As Integer = CInt(s.Length / 2) Dim bytes(arraySize - 1) As Byte Dim counter As Integer For i As Integer = 0 To s.Length - 1 Step 2 Dim hexValue As String = s.Substring(i, 2) ' Tell convert to interpret the string as a 16 bit hex value Dim intValue As Integer = Convert.ToInt32(hexValue, 16) ' Convert the integer to a byte and store it in the array bytes(counter) = Convert.ToByte(intValue) counter += 1 Next Return bytes End Function
Yes, your hex data smells like a JPEG image. There is no need to write it to disk, you could use a MemoryStream instead. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.