Results 1 to 7 of 7

Thread: (Please I know this has been covered but...) Open, Save Binary, convert to/from Hex

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Location
    NC, USA
    Posts
    102

    Question

    Please I am begging. I have looked through so many articles and so many discussions that my eyes are about to cross. I can not get this to work. I am trying to open a file with a .bin extention. I assume it is binary, right? It opens in a hex editor just fine. I want to be able to open it, read certain chunks of data and display them as Hexadecimal, take options and selections from the user as hex data combine everything in a specific order and write it out as a new binary file. The binary files are relatively small at 4k per file. What I am having a problem with is this. I open the file for binary just like I read. when I look at the value it is in some kind of extended ascii format or something. I thought it was supposed to be 1's and 0's? It really wouldnt matter to me what it was since all I want to deal with is hex and let a conversion code handle the going back and fourth between bin and hex. So I have tried almost every single code sample I can find for (and there are plenty of them). But all of them fail and give me errors. I assume it is because they are expecting to see 1's and 0's and instead are being passed those extended ascii or whatever they are. Is it obvious what I am I doing wrong? Any pointers would be greatly appreciated. or a code sample of reading 1 byte from binary and showing as a two char hex string would be awesome.

    Thanks in Advance for any help,
    Richard

  2. #2
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    455
    Hello KillemAll,

    The extention of a file is saying nothing about what kind of file it is.
    The content of a file is allways hexadecimal.
    It is the program what is establishing what to do with the file content.
    Every time you reading the smallest part of the file, it is a byte!
    If you want to make bits of it, you have to convert it.

    I do hope this will help you.
    If I don't understand your question please say with other words, maybe I can help you.

    Nice regards,

    Michelle.

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    A file is always a file, it's how you read it that will decide what format it is.
    a "binary" file is probably a file stored with binary contents of variables so yes, binary is the correct
    method to read that file. I assume you don't have any specific variables known there so you may load it into a byte array
    Code:
    Dim buffer() as byte
    Open filepath for binary as #1
      Get#1,,buffer
    close #1
    debug.print "The hex value of the 42'nd byte in the file is: " & Hex(buffer(42))
    Now the last line will show you how to convert it to hex, you use Hex() function.
    Also if you want to convert a string character to hex then use Hex(ASc())
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Location
    NC, USA
    Posts
    102

    Thumbs up Thanks I think I got it now

    This may not be the best way but it does appear to work properly. It was returning some hex values as only one char so I had to get it to buffer with a leading 0 for those. I have not tried converting back from the hex value to the Binary value. I think I understand enough now to get it to work though. If you think this code could be done better please let me know.

    Thanks,
    Richard

    '<-------Begin Code------->
    Const ChunkSize = 1
    Dim Data As String
    Dim HexData As String

    Public Sub Form_Load()
    Me.Show
    Open "C:\Test.bin" For Binary As #1
    Do Until LOF(1) = Loc(1) Or EOF(1)
    Data = ""
    If LOF(1) - Loc(1) < ChunkSize Then
    Data = String(LOF(1) - Loc(1), 0)
    Else
    Data = String(ChunkSize, 0)
    End If
    Get #1, , Data
    If Len(Hex(Asc(Data))) = 1 Then
    HexData = "0" & Hex(Asc(Data)) & " "
    ElseIf Len(Hex(Asc(Data))) = 2 Then
    HexData = Hex(Asc(Data)) & " "
    Else
    MsgBox "(" & Hex(Asc(Data)) & ") was returned", vbExclamation, "There was an error"
    End If
    Text1.Text = Text1.Text & HexData
    Loop
    End Sub
    '<-------End Code------->

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Location
    NC, USA
    Posts
    102

    Talking Thanks again

    This one does both the reads and writes. You could modify the hex values of the array of strings between the read and save, but this was just getting the basic conversions down. No dlls, no long modules for the conversion, no exotic formulas, you use the Hex(Asc()) to convert from that extended ascii type character that the "Open filename for Binary As ..." command returns to decimal and then hex. store each one in an array of strings and then use Chr(Val("&H" & )) to convert back from hex to decimal and then from decimal back to the extended ascii character type. works like a champ.

    Thanks again,
    Richard

    '<-------Begin Code------->
    Dim Data As String
    Dim HexData As String
    Dim HexArray(4095) As String * 2

    Private Sub cmdDisplay_Click()
    Dim iCount As Integer
    For iCount = 0 To 4095
    Text1.Text = Text1.Text & HexArray(iCount) & " "
    Next iCount
    End Sub

    Public Sub cmdRead_Click()
    Open "C:\Test.bin" For Binary As #1
    Do Until LOF(1) = Loc(1) Or EOF(1)
    Data = ""
    Data = String(1, 0)
    Get #1, , Data
    If Len(Hex(Asc(Data))) = 1 Then
    HexData = "0" & Hex(Asc(Data))
    ElseIf Len(Hex(Asc(Data))) = 2 Then
    HexData = Hex(Asc(Data))
    Else
    MsgBox "(" & Hex(Asc(Data)) & ") was returned", vbExclamation, "There was an error"
    Exit Sub
    End If
    HexArray(Loc(1) - 1) = HexData
    Loop
    End Sub

    Private Sub cmdSave_Click()
    Open "C:\New.bin" For Binary As #2
    Dim iCount As Integer
    For iCount = 0 To 4095
    Dim binstring2 As String * 1
    binstring2 = Chr(Val("&H" & HexArray(iCount)))
    Put #2, , binstring2
    Next iCount
    End Sub

    '<-------End Code------->

    Again, I am sure there is probably a more efficient way to do some of it but this worked for me. Btw, if you try this code you should set the text1.multiline true and the text1.font to fixedsys.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Code:
    Private Sub cmdDisplay_Click()
        Dim icount As Integer, temp As String
        For icount = 0 To 4095
            temp = temp & HexArray(icount) & " " 'Now it would be even faster with Copymemory
        Next icount
        text1 = temp
    End Sub
    
    Private Sub cmdSave_Click()
    Dim Binarray(4095) As Byte, icount As Integer
        For icount = 0 To 4095 'A byte array instead
            Binarray(icount) = Val("&H" & HexArray(icount))
        Next icount
        Open "C:\New.bin" For Binary As #2
            Put #2, , Binarray 'This is a lot faster than putting a byte one by one
        Close #2
    End Sub
    Here's some improovement i did for your cmdSave and cmdDisplay functions, well cmdread should also be done with a byte array instead...
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Location
    NC, USA
    Posts
    102

    Smile Thanks,

    Thanks,
    You are "The Man". I appreciate the constructive feedback.

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