|
-
Aug 4th, 2004, 08:48 AM
#1
Thread Starter
Member
Binary --> Hex.....[Resolved]
Is there anybody knows how to convert Binary file (e.g. .gif, .bmp) into Hex code?
Please help
Last edited by Siu Yan; Aug 4th, 2004 at 11:34 AM.
Quitters never Win, Winners never Quit, But those who Never Win and Never Quit are Idiots 
-
Aug 4th, 2004, 08:50 AM
#2
Binary, octal, digital, hex - all different ways to "display" a number.
So, I'm not understanding you question.
-
Aug 4th, 2004, 08:55 AM
#3
Thread Starter
Member
I want to know how to write a VB code to get Hex code from a binary file. This binary file maybe a .gif / .bmp file
E.g. a picture --> process --> Hex code (numbers)
How to do it?
Quitters never Win, Winners never Quit, But those who Never Win and Never Quit are Idiots 
-
Aug 4th, 2004, 10:38 AM
#4
Need-a-life Member
VB Code:
Private Function TranslateFileIntoBinary(sContents As String)
Dim i As Long
Dim sHex As String
sHex = ""
For i = 1 To Len(sContents)
sHex = sHex & Hex(Asc(Mid$(sContents, i, 1)))
Next i
TranslateFileIntoBinary = sHex
End Function
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Aug 4th, 2004, 10:54 AM
#5
Thread Starter
Member
Thanks Mc Brain....
I want to change a picture / image into binary but your code is changing a string into binary...It doesn't really that helping me...
Thanks anyway.
Quitters never Win, Winners never Quit, But those who Never Win and Never Quit are Idiots 
-
Aug 4th, 2004, 10:58 AM
#6
I think we are both not understanding your question.
That function will give you the HEX display of the contents of a string. Read the file into the string and use the function - and you will see it's HEX values.
-
Aug 4th, 2004, 11:03 AM
#7
Need-a-life Member
Originally posted by szlamany
I think we are both not understanding your question.
That function will give you the HEX display of the contents of a string. Read the file into the string and use the function - and you will see it's HEX values.
Translation 
VB Code:
Private Sub Form_Load()
Dim sContents As String
Open "C:\Picture.gif" For Binary As #1
sContents = Input(LOF(1), 1)
Close #1
Debug.Print TranslateFileIntoBinary(sContents)
End Sub
Private Function TranslateFileIntoBinary(sContents As String)
Dim i As Long
Dim sHex As String
sHex = ""
For i = 1 To Len(sContents)
sHex = sHex & Hex(Asc(Mid$(sContents, i, 1)))
Next i
TranslateFileIntoBinary = sHex
End Function
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Aug 4th, 2004, 11:33 AM
#8
Thread Starter
Member
Thanks
Quitters never Win, Winners never Quit, But those who Never Win and Never Quit are Idiots 
-
Aug 5th, 2004, 12:58 PM
#9
There is actually a lot faster method of doing this:
VB Code:
'in a module
Public Function OpenFileAsHex(ByVal Filename As String) As String
Dim ReadBuffer() As Byte, ReturnBuffer() As Byte
Dim FileNumber As Byte, A As Long, B As Long
FileNumber = FreeFile
'open file
Open Filename For Binary Access Read As #FileNumber
'prepare a read buffer
ReDim Preserve ReadBuffer(LOF(FileNumber) - 1)
'prepare a return buffer
ReDim Preserve ReturnBuffer(LOF(FileNumber) * 3 - 1)
'read all data
Get #FileNumber, , ReadBuffer
Close #FileNumber
'convert numbers to 0 - F
For A = 0 To UBound(ReturnBuffer) Step 3
'divide by three (\ is faster than /)
B = A \ 3
'&H30 = 48, which is the character code for 0
'the code before it rips the four upper bits (value will be 0 - 15)
ReturnBuffer(A) = ((ReadBuffer(B) And &HF0) \ &H10) Or &H30
'rip the four lower bits and and add 48
ReturnBuffer(A + 1) = (ReadBuffer(B) And &HF) Or &H30
'space character to divide the hex values from each other
ReturnBuffer(A + 2) = &H20
'then check if character code is bigger than the code for number 9
'and add seven if it is so 10 will be A and 11 B and so on
If ReturnBuffer(A) > 57 Then ReturnBuffer(A) = ReturnBuffer(A) + 7
If ReturnBuffer(A + 1) > 57 Then ReturnBuffer(A + 1) = ReturnBuffer(A + 1) + 7
Next A
'convert to string
OpenFileAsHex = StrConv(ReturnBuffer, vbUnicode)
End Function
Usage:
Text1.Text = OpenFileAsHex("c:\autoexec.bat")
(this still isn't the fastest possible method, but very much faster than the one posted above - remember to compile to EXE if you want to see some real speed! )
-
Aug 5th, 2004, 01:00 PM
#10
Very neat code - but wow - how did you ever come up with all that?
-
Aug 5th, 2004, 01:08 PM
#11
By annoying enough one guy who was obsessed with optimizing his code so that he showed a way for some nice routines. He even posted an assembly optimized type library which works far faster than StrConv 
Anyways, this is nowadays my basic way of thinking: process data in byte arrays, and use strings only when you have no other choice.
Also, I just made that code
-
Aug 5th, 2004, 01:12 PM
#12
We've only been a VB shop for three years now - came from DIGITAL mainframe/mini VAX's before that.
We need to convert a "scheduling algorithm" (for putting high school students into classes) in the next few months - speed will be a huge issue for us.
Been doing user interface programs in VB mostly so far - so speed hasn't been a challenge...
-
Aug 5th, 2004, 01:26 PM
#13
As long as you can stay in simple math, you should have no impossible speed issues. Unlike some people tend to say, math in VB is just as good as it is in C - just compile the code to see the speed 
And always when testing a speed of the code, do it only when the code is compiled.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|