Is there anybody knows how to convert Binary file (e.g. .gif, .bmp) into Hex code?
Please help :confused:
Printable View
Is there anybody knows how to convert Binary file (e.g. .gif, .bmp) into Hex code?
Please help :confused:
Binary, octal, digital, hex - all different ways to "display" a number.
So, I'm not understanding you question.
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?
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
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. :(
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 ;)Quote:
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.
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
Thanks :thumb:
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! :))
Very neat code - but wow - how did you ever come up with all that? :D
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 :D
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 :)
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...
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.