locked
Convert text file data to Hex file data.

    Question

  • Hello,

    I have a hex listing in a text file that is 14 bytes long...

    40 00 78 7B 7A

    These are characters. The hex values are...

    34 30 20
    30 30 20
    37 38 20
    37 42 20
    37 61

    How do I go about converting this to a file containing only hex values? The resulting file should contain only 5 bytes...

    4000787B7A

    Thank you in advance,

    ED

    Sunday, November 05, 2006 8:19 PM

Answers

  • You may wish to take a look at the hex function:

    (from the object browser)

    Public Shared Function Hex(ByVal Number As Integer) As String

    Member of Microsoft.VisualBasic.Conversion

    Summary:

    Returns a string representing the hexadecimal value of a number.

    Parameters:

    Number: Required. Any valid numeric expression or String expression.

    Return Values:

    Returns a string representing the hexadecimal value of a number.

    Public Shared Function Hex(ByVal Number As Byte) As String

    Member of Microsoft.VisualBasic.Conversion

    Summary:

    Returns a string representing the hexadecimal value of a number.

    Parameters:

    Number: Required. Any valid numeric expression or String expression.

    Return Values:

    Returns a string representing the hexadecimal value of a number.

    Monday, November 06, 2006 2:53 PM
  • Hello,

    Thanks for the tip.

    I looked at the Hex function, but it is used to convert a number to the string representation:

    hex(123)
    "7B"

    I did find ToByte. It does just what I need. Your tip pointed the way. Here is the code as it now stands.

    Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim i As Integer
    Dim st As String = ""
    Dim s() As Byte = System.IO.File.ReadAllBytes("c:\b.txt")
    Dim hextemp As Integer
    Dim hexstring1 As String
    Dim hexdata() As Byte
    Dim count As Integer = -1

    For x As Integer = 0 To s.Length - 1
        i = CType(s(x), Integer)
        st = st + Chr(i)
        Next

    st = st.Replace(" ", "")

    For i = 1 To st.Length Step 2
        count += 1
        ReDim Preserve hexdata(count)
        hexstring1 = Mid(st, i, 2)
        hextemp = Convert.ToByte(hexstring1, 16)
        hexdata(count) = hextemp
        Next
    System.IO.File.WriteAllBytes("c:\b.hex", hexdata)
    End Sub
    End Class

    Thanks for the help guys,

    ED

    Monday, November 06, 2006 9:55 PM

All replies

  • Public Class Form1

     

        Private Sub Button1_Click(ByVal sender As System.Object, _

        ByVal e As System.EventArgs) Handles Button1.Click

            ' Where test3.fil contains the text:

            ' 3430203030203738203742203761

            Dim i As Integer

            Dim j As String = ""

            Dim st As String = ""

            Dim s() As Byte = System.IO.File.ReadAllBytes("c:\test3.fil")

            Dim r(s.Length) As Char

            For x As Integer = 0 To s.Length - 1

                i = CType(s(x), Integer)

                st = st + Chr(i)

            Next

            st = st.Replace("20", "")

            For i = 1 To st.Length Step 2

                Dim g As Integer = CInt(Mid(st, i, 2))

                j = j + Chr(Convert.ToInt32(g.ToString, 16))

            Next

            ' Your result in a msgbox

            MsgBox(j)

        End Sub

     

     

    End Class

     

    Monday, November 06, 2006 4:42 AM
  • Hello,

    Thanks for the sample.

    I was having a problem with your code when getting to a value that contained a letter (e.g.: 7B).

    I modified your sample, and using brute force, I was able to get it working using two select/case blocks. Here is the code I put together.

    Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim i As Integer
    Dim j As String = ""
    Dim st As String = ""
    Dim s() As Byte = System.IO.File.ReadAllBytes("c:\b.txt")
    Dim r(s.Length) As Char
    Dim hextemp As Integer
    Dim hexstring1 As String
    Dim hexstring2 As String
    Dim hexdata() As Byte
    Dim count As Integer = -1

    For x As Integer = 0 To s.Length - 1
        i = CType(s(x), Integer)
        st = st + Chr(i)
        Next
    st = st.Replace(" ", "")

    For i = 1 To st.Length Step 2
        count += 1
        ReDim Preserve hexdata(count)
        hexstring1 = Mid(st, i, 1)
        hexstring2 = Mid(st, i + 1, 1)
        Select Case hexstring1
            Case "0" : hextemp = &H0
            Case "1" : hextemp = &H10
            Case "2" : hextemp = &H20
            Case "3" : hextemp = &H30
            Case "4" : hextemp = &H40
            Case "5" : hextemp = &H50
            Case "6" : hextemp = &H60
            Case "7" : hextemp = &H70
            Case "8" : hextemp = &H80
            Case "9" : hextemp = &H90
            Case "A" : hextemp = &HA0
            Case "B" : hextemp = &HB0
            Case "C" : hextemp = &HC0
            Case "D" : hextemp = &HD0
            Case "E" : hextemp = &HE0
            Case "F" : hextemp = &HF0
            End Select
        Select Case hexstring2
            Case "0" : hextemp += &H0
            Case "1" : hextemp += &H1
            Case "2" : hextemp += &H2
            Case "3" : hextemp += &H3
            Case "4" : hextemp += &H4
            Case "5" : hextemp += &H5
            Case "6" : hextemp += &H6
            Case "7" : hextemp += &H7
            Case "8" : hextemp += &H8
            Case "9" : hextemp += &H9
            Case "A" : hextemp += &HA
            Case "B" : hextemp += &HB
            Case "C" : hextemp += &HC
            Case "D" : hextemp += &HD
            Case "E" : hextemp += &HE
            Case "F" : hextemp += &HF
            End Select
        hexdata(count) = hextemp
        Next
    System.IO.File.WriteAllBytes("c:\b.hex", hexdata)
    End Sub
    End Class

    I'm sure there is a way to simplify the Select statements but it works.

    Thanks again,

    ED

    Monday, November 06, 2006 2:21 PM
  • You may wish to take a look at the hex function:

    (from the object browser)

    Public Shared Function Hex(ByVal Number As Integer) As String

    Member of Microsoft.VisualBasic.Conversion

    Summary:

    Returns a string representing the hexadecimal value of a number.

    Parameters:

    Number: Required. Any valid numeric expression or String expression.

    Return Values:

    Returns a string representing the hexadecimal value of a number.

    Public Shared Function Hex(ByVal Number As Byte) As String

    Member of Microsoft.VisualBasic.Conversion

    Summary:

    Returns a string representing the hexadecimal value of a number.

    Parameters:

    Number: Required. Any valid numeric expression or String expression.

    Return Values:

    Returns a string representing the hexadecimal value of a number.

    Monday, November 06, 2006 2:53 PM
  • Hello,

    Thanks for the tip.

    I looked at the Hex function, but it is used to convert a number to the string representation:

    hex(123)
    "7B"

    I did find ToByte. It does just what I need. Your tip pointed the way. Here is the code as it now stands.

    Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim i As Integer
    Dim st As String = ""
    Dim s() As Byte = System.IO.File.ReadAllBytes("c:\b.txt")
    Dim hextemp As Integer
    Dim hexstring1 As String
    Dim hexdata() As Byte
    Dim count As Integer = -1

    For x As Integer = 0 To s.Length - 1
        i = CType(s(x), Integer)
        st = st + Chr(i)
        Next

    st = st.Replace(" ", "")

    For i = 1 To st.Length Step 2
        count += 1
        ReDim Preserve hexdata(count)
        hexstring1 = Mid(st, i, 2)
        hextemp = Convert.ToByte(hexstring1, 16)
        hexdata(count) = hextemp
        Next
    System.IO.File.WriteAllBytes("c:\b.hex", hexdata)
    End Sub
    End Class

    Thanks for the help guys,

    ED

    Monday, November 06, 2006 9:55 PM