Results 1 to 9 of 9

Thread: Write Hex Value to Binary File

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    5

    Write Hex Value to Binary File

    Hi, I am not exactly sure how to say this, but what I want to do is to write a series of strings converted to their hex notation to a binary file. Here is what I have:

    Code:
    Dim hexNumbers As System.Text.StringBuilder = New System.Text.StringBuilder
    
        Private Sub add_byte(ByVal str As String)
            hexNumbers.Append(String.Format("{0:x}", Convert.ToChar(str)))
        End Sub
    
        Private Sub add_short(ByVal str As String)
            hexNumbers.Append(String.Format("{0:x}", Convert.ToInt16(str)))
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim fstream As New FileStream(Application.StartupPath & "\help.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite)
            Dim bw As BinaryWriter = New BinaryWriter(fstream)
    
            add_byte(DataGridView1.Rows(0).Cells(0).Value)
            add_byte(DataGridView1.Rows(0).Cells(1).Value)
            add_byte(DataGridView1.Rows(0).Cells(2).Value)
            add_byte(DataGridView1.Rows(0).Cells(3).Value)
            add_byte(DataGridView1.Rows(0).Cells(4).Value)
            add_short(DataGridView1.Rows(0).Cells(5).Value)
            add_short(DataGridView1.Rows(0).Cells(6).Value)
    
            bw.Write(String.Format("{0:x}", hexNumbers)) '<-- doesn't change a thing
    
            bw.Close()
    
            MsgBox("Done")
        End Sub
    Looking at a hex editor I see:

    Code:
    30 30 31 30        0 0 1 0
    What I was going for was: 00 00 16 to be converted to 00 00 10 and have that value written to the binary file instead of the above 30 30 31 30.

    So, what do I need to do to have the actual hex value written to the binary rather than it's ASCII equivalents?

    Thanks

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Write Hex Value to Binary File

    1) There are no such thing as 'hex value'. A value is a value. You can represent it in decimal form, in hexadecimal form, or even in the Maya language but it would still remain the same.
    For example, the value of 20 is: 10100 in binary, 24 in octal, 14 in hexadecimal or XX in Roman digits. Regardless of the representation XX + 1 will be 21.

    2) A string is represented in memory as collection of bytes. Each letter is coded with 1 (ASCII) or 2 (Unicode) bytes. A computer memory is a large organized collection of cells that can hold electric charge that means if charge is present the cell holds 1 and if there is no charge the cell holds 0. Each cell is called bit, 8 bits makes a byte. Hex representation you see in hex editor is simply a human readable representation of binary contents.

    3) Finally, considering 1) and 2) you don't have to convert your string into hex values. If you simply write an ASCII string HELLO to a file you'll see 48 45 4C 4C 4F in it. So you can simply use bw.Write to write your string.

    4) And if you need to convert a string into a byte array you can use System.Text.Encoding class for that.
    For ASCII encoding you can simply use:

    Code:
    Dim myBytes() As Byte = System.Text.Encoding.ASCII.GetBytes("HELLO")

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    5

    Re: Write Hex Value to Binary File

    Hi, first off, thanks for the detailed information. Very helpful.

    Now, the thing is that when I process the 16, I convert it to it's hex notation, and when I write that value (i.e. 10) I get back 31 30. If I open this file in a hex editor, I want to see 10, not 31 30. How do I fix that?
    Last edited by montrom; May 5th, 2010 at 01:55 AM.

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Write Hex Value to Binary File

    That's because you don't write the value 10 into your file you write two ASCII symbols "1" and "0" instead. Their correspondent ASCII codes are 0x31 and 0x30.

    To write the value of 16:
    Code:
    Dim b As Byte = 16
    bw.Write(b)

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    5

    Re: Write Hex Value to Binary File

    Thanks again. One thing I noticed is that when I write a string using:

    Code:
      Private Sub write_text(ByVal str As String)
            bw.Write(String.Format("{0:x}", Convert.ToChar(str)))
        End Sub
    The writer puts a 01 in front of each hex notation. So, for instance, instead of HELLO, I get 01 H 01 E 01 L 01 L 01 O. What's the trick to getting rid of those 01's?

  6. #6
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Write Hex Value to Binary File

    0:x - x is intended for conversion of the numeric values. Char and Text is NOT numeric.
    Can't you simply write like this:

    Code:
    bw.Write(str)
    ' Correction - using the code above the writer will add the length of the string before it.
    Use this:

    Code:
    bw.Write(System.Text.Encoding.ASCII.GetBytes(str))

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    5

    Re: Write Hex Value to Binary File [SOLVED]

    No, but I figured it out. I just had to get rid of the string.format portion and this works:

    Code:
     bw.Write(Convert.ToChar(str))
    Thanks again.

  8. #8
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Write Hex Value to Binary File [SOLVED]

    Quote Originally Posted by montrom View Post
    No, but I figured it out. I just had to get rid of the string.format portion and this works:

    Code:
     bw.Write(Convert.ToChar(str))
    Thanks again.
    This code will only work if str is EXACTLY 1 character long. Any longer string will throw an exception.
    I repeat, to write text of any length to a file using binary reader use this:
    Code:
    bw.Write(System.Text.Encoding.ASCII.GetBytes(str))

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    5

    Re: Write Hex Value to Binary File

    Even better.

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