Results 1 to 13 of 13

Thread: Binary --> Hex.....[Resolved]

  1. #1

    Thread Starter
    Member Siu Yan's Avatar
    Join Date
    Apr 2004
    Location
    Hong Kong --> Ireland
    Posts
    49

    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

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    Binary, octal, digital, hex - all different ways to "display" a number.

    So, I'm not understanding you question.

  3. #3

    Thread Starter
    Member Siu Yan's Avatar
    Join Date
    Apr 2004
    Location
    Hong Kong --> Ireland
    Posts
    49
    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

  4. #4
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    VB Code:
    1. Private Function TranslateFileIntoBinary(sContents As String)
    2.     Dim i As Long
    3.     Dim sHex As String
    4.    
    5.     sHex = ""
    6.     For i = 1 To Len(sContents)
    7.         sHex = sHex & Hex(Asc(Mid$(sContents, i, 1)))
    8.     Next i
    9.    
    10.     TranslateFileIntoBinary = sHex
    11. 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.

  5. #5

    Thread Starter
    Member Siu Yan's Avatar
    Join Date
    Apr 2004
    Location
    Hong Kong --> Ireland
    Posts
    49
    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

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    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.

  7. #7
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    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:
    1. Private Sub Form_Load()
    2.     Dim sContents As String
    3.  
    4.     Open "C:\Picture.gif" For Binary As #1  
    5.     sContents = Input(LOF(1), 1)
    6.     Close #1
    7.  
    8.     Debug.Print TranslateFileIntoBinary(sContents)
    9. End Sub
    10.  
    11. Private Function TranslateFileIntoBinary(sContents As String)
    12.     Dim i As Long
    13.     Dim sHex As String
    14.    
    15.     sHex = ""
    16.     For i = 1 To Len(sContents)
    17.         sHex = sHex & Hex(Asc(Mid$(sContents, i, 1)))
    18.     Next i
    19.    
    20.     TranslateFileIntoBinary = sHex
    21. 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.

  8. #8

    Thread Starter
    Member Siu Yan's Avatar
    Join Date
    Apr 2004
    Location
    Hong Kong --> Ireland
    Posts
    49
    Thanks
    Quitters never Win, Winners never Quit, But those who Never Win and Never Quit are Idiots

  9. #9
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    There is actually a lot faster method of doing this:

    VB Code:
    1. 'in a module
    2. Public Function OpenFileAsHex(ByVal Filename As String) As String
    3.     Dim ReadBuffer() As Byte, ReturnBuffer() As Byte
    4.     Dim FileNumber As Byte, A As Long, B As Long
    5.     FileNumber = FreeFile
    6.     'open file
    7.     Open Filename For Binary Access Read As #FileNumber
    8.         'prepare a read buffer
    9.         ReDim Preserve ReadBuffer(LOF(FileNumber) - 1)
    10.         'prepare a return buffer
    11.         ReDim Preserve ReturnBuffer(LOF(FileNumber) * 3 - 1)
    12.         'read all data
    13.         Get #FileNumber, , ReadBuffer
    14.     Close #FileNumber
    15.     'convert numbers to 0 - F
    16.     For A = 0 To UBound(ReturnBuffer) Step 3
    17.         'divide by three (\ is faster than /)
    18.         B = A \ 3
    19.         '&H30 = 48, which is the character code for 0
    20.         'the code before it rips the four upper bits (value will be 0 - 15)
    21.         ReturnBuffer(A) = ((ReadBuffer(B) And &HF0) \ &H10) Or &H30
    22.         'rip the four lower bits and and add 48
    23.         ReturnBuffer(A + 1) = (ReadBuffer(B) And &HF) Or &H30
    24.         'space character to divide the hex values from each other
    25.         ReturnBuffer(A + 2) = &H20
    26.         'then check if character code is bigger than the code for number 9
    27.         'and add seven if it is so 10 will be A and 11 B and so on
    28.         If ReturnBuffer(A) > 57 Then ReturnBuffer(A) = ReturnBuffer(A) + 7
    29.         If ReturnBuffer(A + 1) > 57 Then ReturnBuffer(A + 1) = ReturnBuffer(A + 1) + 7
    30.     Next A
    31.     'convert to string
    32.     OpenFileAsHex = StrConv(ReturnBuffer, vbUnicode)
    33. 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! )

  10. #10
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    Very neat code - but wow - how did you ever come up with all that?

  11. #11
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    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

  12. #12
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    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...

  13. #13
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    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
  •  



Click Here to Expand Forum to Full Width