Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Convert an Image, encoded in hex, back to an image file

Convert an Image, encoded in hex, back to an image file

Scheduled Pinned Locked Moved Visual Basic
question
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ESTAN
    wrote on last edited by
    #1

    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.

    E 1 Reply Last reply
    0
    • E ESTAN

      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.

      E Offline
      E Offline
      ESTAN
      wrote on last edited by
      #2

      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

      L 1 Reply Last reply
      0
      • E ESTAN

        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

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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.


        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups