You need to convert an image of a hex number into a hex string? Or you need to convert the image file into a (very long) hex string?
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read. Please Help Us To Save Ana
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Well as far as you and I are concerned nothing but I have seen other threads around here where they were taboo'd. I think it has to do something with making a program to break security measures. That's usually the reason for the application.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
I would like to kindly ask how do you convert the HEX representation back to the jpg?
Do you know?
The original jpg file was simply converted to a hex string just like a hex dump application would do. You would have to have a application that understands image formats and so you would tell it to take this raw hex file and make a jpg image out of it. Perhaps that can be done. I suppose a program could take any hex data string and convert it into any image file you want. Anything other than the original image source format would of could come out a bizzar image.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
In that case you haven't tried it... It works perfectly well when I run it. So can you tell me what's wrong with it.
I used your code to convert a jpg image. I also used a reliable hex dump application that I have and did the same thing. The two hex files are the same but yours is done in a hex-character format (for easy viewing) and the other is in pure hex but the same values.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Joacim,
Thanks man, will try the code later and let you know.
Actually i did notice the age of the thread cos i was worried you guys might not update it anymore since i asked for help 2 year later.
I haven't used VB6 for several years but it should look something like this
Code:
Dim iFile As Integer
iFile = FreeFile
Open sSomeStringContainingFilename For Binary Access Write As #iFile
Write #iFile, bTheByteArrayReturnedFromTheFunction
Close #iFile
Please, it display path/file access error.Run time error 75.
Thanks.
Originally Posted by Joacim Andersson
I haven't used VB6 for several years but it should look something like this
Code:
Dim iFile As Integer
iFile = FreeFile
Open sSomeStringContainingFilename For Binary Access Write As #iFile
Write #iFile, bTheByteArrayReturnedFromTheFunction
Close #iFile
I'm supposed to store the jpg in hex format and later make it a jpg again.... any idea?
I'm not sure what your really want but below code does two things
1) reads in a GIF file (or any image file), converts it to Hex String (like you see in a dump) and writes it out as a file.
2) reads in the Hex String file and converts it back again to the original GIF (or any image file) and write that out to a file
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Do not click on Command2 until you have clicked on Command1 first
Code:
Private Sub Command1_Click()
'
' Image To Hex String
'
' Read in an image file
' Convert it to Hex String: "474946383961C800B90087260........."
' Write the HeX String to a file
'
Dim s As String
s = Image2Hex(App.Path & "\t-rex.gif")
Open App.Path & "\t-rex_hex.txt" For Binary As #1
Put #1, 1, s
Close #1
End Sub
Private Sub Command2_Click()
'
' Hex String To Image
'
' Read in the Hex String file created with Command1
' Convert it back to the same binary code as the original
' Write it out to a file
'
Dim s As String
Dim b() As Byte
'
' Open and read the hex string file created with Command1
'
Open App.Path & "\t-rex_hex.txt" For Binary As #2
s = String(LOF(2), 0)
Get #2, 1, s
Close #2
'
' Convert it to a byte array
'
b = Hex2ByteArr(s)
'
' Write it out to a file as an image file
'
Open App.Path & "\New_t-rex.gif" For Binary As #3
Put #3, 1, b
Close #3
End Sub
Private Function ByteArr2Hex(bArr() As Byte) As String
Dim sHex As String, n As Long, nCount As Long
nCount = UBound(bArr)
sHex = String((nCount + 1) * 2, " ")
For n = 0 To nCount
Mid$(sHex, n * 2 + 1, 2) = Right$("0" & Hex(bArr(n)), 2)
Next
ByteArr2Hex = sHex
End Function
Public Function Image2Hex(ByVal sFileName As String) As String
Dim hFile As Integer, bArr() As Byte
On Error Resume Next
If Len(Dir$(sFileName, vbHidden Or vbSystem)) = 0 Then
Call MsgBox("Can't find the file", vbCritical, "Error")
Exit Function
End If
hFile = FreeFile
Open sFileName For Binary Access Read As #hFile
If Err.Number = 0 Then
ReDim bArr(LOF(hFile) - 1)
Get #hFile, , bArr
Image2Hex = ByteArr2Hex(bArr)
Else
Call MsgBox("Can't open the file", vbCritical, "Error")
End If
Close #hFile
End Function
Public Function Hex2ByteArr(sHex As String) As Byte()
'
' Call this function and write the byte array it returns back to disk
'
Dim bArr() As Byte
Dim nUBound As Long, n As Long
sHex = Replace(sHex, " ", "")
'Since each byte takes up 2 hex numbers the string length must be dividable by 2
If Not Len(sHex) Mod 2 = 0 Then
Exit Function
End If
nUBound = Len(sHex) \ 2 - 1
ReDim bArr(nUBound)
For n = 0 To nUBound
k = CInt("&H" & Mid$(sHex, n * 2 + 1, 2))
bArr(n) = k
Next
Hex2ByteArr = bArr
End Function
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Please, below is the code. I want to convert the image(jpeg) which was loaded in the image box to hex and then convert the hex value back to the original image which will appear in the image box also.
Thanks.
Private Sub cmdImageToHex_Click()
Dim s As String
s = Image2Hex(Image1.Picture)
Open App.Path & "\t_rex_hex.txt" For Binary As #1
Put #1, 1, s
Close #1
End Sub
Private Function ByteArr2Hex(bArr() As Byte) As String
Dim sHex As String, n As Long, nCount As Long
nCount = UBound(bArr)
'Set the reuired length of the string. This is faster than concatinating the string.
'This code assumes the byte array is zero based
sHex = String((nCount + 1) * 2, " ")
For n = 0 To nCount
'insert the hex value into the string
Mid$(sHex, n * 2 + 1, 2) = Right$("0" & Hex(bArr(n)), 2)
Next
ByteArr2Hex = sHex
End Function
Public Function Image2Hex(ByVal sFileName As String) As String
Dim hFile As Integer, bArr() As Byte
On Error Resume Next
If Len(Dir$(sFileName, vbHidden Or vbSystem)) = 0 Then
Call MsgBox("Can't find the file", vbCritical, "Error")
Exit Function
End If
hFile = FreeFile
Open sFileName For Binary Access Read As #hFile
If Err.Number = 0 Then
ReDim bArr(LOF(hFile) - 1)
Get #hFile, , bArr
Image2Hex = ByteArr2Hex(bArr)
Else
Call MsgBox("Can't open the file", vbCritical, "Error")
End If
Close #hFile
End Function
Private Sub cmdHexToImage_Click()
Dim s As String
Dim b() As Byte
Open App.Path & "\t_rex_hex.txt" For Binary As #2
s = String(LOF(2), 0)
'''s = String(LOF(2), 0)
Get #2, 1, s
'''Get #2, 1, s
Close #2
'''b = Hex2ByteArr(s)
b = Hex2ByteArr(s)
Open App.Path & "\new_image.jpg" For Binary As #3
Put #3, 1, b
Image1.Picture = LoadPicture(App.Path & "\new_image.jpg")
Close #3
End Sub
Public Function Hex2ByteArr(sHex As String) As Byte()
'
' Call this function and write the byte array it returns back to disk
'
Dim bArr() As Byte
Dim nUBound As Long, n As Long
'Remove all spaces in the hex string
sHex = Replace(sHex, " ", "")
'Since each byte takes up 2 hex numbers the string length must be dividable by 2
If Not Len(sHex) Mod 2 = 0 Then
'you should throw an error here
Exit Function
End If
nUBound = Len(sHex) \ 2 - 1
ReDim bArr(nUBound + 1)
For n = 0 To nUBound
k = CInt("&H" & Mid$(sHex, n * 2 + 1, 2))
bArr(n) = k 'CInt("&H" & Mid$(sHex, n * 2 + 1, 2))
Next
Please, below is the code. I want to convert the image(jpeg) which was loaded in the image box to hex and then convert the hex value back to the original image which will appear in the image box also.
Thanks.
Private Sub cmdImageToHex_Click()
Dim s As String
s = Image2Hex(Image1.Picture)
Open App.Path & "\t_rex_hex.txt" For Binary As #1
Put #1, 1, s
Close #1
End Sub
Private Function ByteArr2Hex(bArr() As Byte) As String
Dim sHex As String, n As Long, nCount As Long
nCount = UBound(bArr)
'Set the reuired length of the string. This is faster than concatinating the string.
'This code assumes the byte array is zero based
sHex = String((nCount + 1) * 2, " ")
For n = 0 To nCount
'insert the hex value into the string
Mid$(sHex, n * 2 + 1, 2) = Right$("0" & Hex(bArr(n)), 2)
Next
ByteArr2Hex = sHex
End Function
Public Function Image2Hex(ByVal sFileName As String) As String
Dim hFile As Integer, bArr() As Byte
On Error Resume Next
If Len(Dir$(sFileName, vbHidden Or vbSystem)) = 0 Then
Call MsgBox("Can't find the file", vbCritical, "Error")
Exit Function
End If
hFile = FreeFile
Open sFileName For Binary Access Read As #hFile
If Err.Number = 0 Then
ReDim bArr(LOF(hFile) - 1)
Get #hFile, , bArr
Image2Hex = ByteArr2Hex(bArr)
Else
Call MsgBox("Can't open the file", vbCritical, "Error")
End If
Close #hFile
End Function
Private Sub cmdHexToImage_Click()
Dim s As String
Dim b() As Byte
Open App.Path & "\t_rex_hex.txt" For Binary As #2
s = String(LOF(2), 0)
'''s = String(LOF(2), 0)
Get #2, 1, s
'''Get #2, 1, s
Close #2
'''b = Hex2ByteArr(s)
b = Hex2ByteArr(s)
Open App.Path & "\new_image.jpg" For Binary As #3
Put #3, 1, b
Image1.Picture = LoadPicture(App.Path & "\new_image.jpg")
Close #3
End Sub
Public Function Hex2ByteArr(sHex As String) As Byte()
'
' Call this function and write the byte array it returns back to disk
'
Dim bArr() As Byte
Dim nUBound As Long, n As Long
'Remove all spaces in the hex string
sHex = Replace(sHex, " ", "")
'Since each byte takes up 2 hex numbers the string length must be dividable by 2
If Not Len(sHex) Mod 2 = 0 Then
'you should throw an error here
Exit Function
End If
nUBound = Len(sHex) \ 2 - 1
ReDim bArr(nUBound + 1)
For n = 0 To nUBound
k = CInt("&H" & Mid$(sHex, n * 2 + 1, 2))
bArr(n) = k 'CInt("&H" & Mid$(sHex, n * 2 + 1, 2))
Next
Hex2ByteArr = bArr
End Function
That code will not convert an image loaded into an Image control to hex. You can convert the file to hex but not the image itself. I don't know of any code that will do what you are asking.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
That code will not convert an image loaded into an Image control to hex. You can convert the file to hex but not the image itself. I don't know of any code that will do what you are asking.
Do you want to take this hex code and compress it so that the file size is smaller? If that is not what you are asking then your request does not make any sense.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.