locked
Reading From a txt file RRS feed

  • Question

  • Hi,

    if i a have a text file containing the following :-

    2222

    How do i read in two ASCII characters and convert them into a combined hex byte?

    so from my exmple above read in 22 and convert them ino a byte such that the byte is now equal to 22 in hex?

    Thanks,

    Andy



    Student
    Tuesday, June 10, 2008 11:41 AM

Answers



  • Hi Andy,

    9 has the ASCII code of 57 so you should change these two lines.>>

       If temp >= 48 And temp <= 56 Then

      If temp2 >= 48 And temp2 <= 56 Then


    to have 57 instead of 56.

    For proof of this see this internet web page.>>





    Regards,

    John



    My profile picture IS NOT really me. I do not have any facial tattoos or piercings in real life. The Microsoft staff and others that have met me know I look nothing like this AVATAR picture. ;-)
    • Edited by John Anthony Oliver Wednesday, June 11, 2008 8:42 AM Removed quoted text.
    • Marked as answer by Dora9 Wednesday, June 11, 2008 9:18 PM
    Wednesday, June 11, 2008 8:41 AM

  • 1Option Strict On 
    2Imports System.IO 
    3 
    4Public Class Form1 
    5 
    6Private mReadBytes As New List(Of Byte) 
    7 
    8Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    9 
    10Dim ofd As New OpenFileDialog 
    11        ofd.Title = "Load File" 
    12        ofd.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*" 
    13        ofd.FileName = "" 
    14        ofd.FilterIndex = 1 
    15        ofd.InitialDirectory = "MyDocuments" 
    16        ofd.CheckFileExists = True 
    17        ofd.CheckPathExists = True 
    18        Try 
    19            If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then 
    20 
    21                Dim myStreamReader As New StreamReader(ofd.FileName) 
    22     
    23                Do While myStreamReader.EndOfStream = False 
    24 
    25                    Dim temp As String 
    26                    Dim temp2 As String 
    27                    temp = CStr(myStreamReader.Read) 
    28                    temp2 = CStr(myStreamReader.Read) 
    29                    Dim total As Integer = Val(Chr(CInt(Val(temp)))) * 16 
    30                    total += Val(Chr(CInt(Val(temp2)))) 
    31                    mReadBytes.Add(Convert.ToByte(total)) 
    32 
    33                Loop 
    34                myStreamReader.Close() 
    35            End If 
    36 
    37        Catch ex As Exception 
    38            MsgBox(ex.Message.ToString, MsgBoxStyle.Information, "Error") 
    39        End Try 
    40 
    41'This section will show the HEX as ASCII text.>
    42Dim myArray() As Byte = mReadBytes.ToArray 
    43Dim output As String = "" 
    44For num As Integer = 0 To myArray.GetUpperBound(0) 
    45output &= Chr(Convert.ToInt32(mReadBytes(num))) 
    46Next 
    47MessageBox.Show(output, "Result....") 
    48 
    49End Sub 
    50End Class 
    51 
    52 

    Hi Andy,

    In the above code I am not using any IF THEN statements other than.>>

     If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then



    I still multiply by 16 to get the HighByte then add the LowByte in the same way.

    So if the text file contains 41 42 like this.>>

    4142

    this translates to 65 and 66 respectively in base 10.

    These are the characters AB in text format from the ASCII character table at.>>





    Regards,

    John


    My profile picture IS NOT really me. I do not have any facial tattoos or piercings in real life. The Microsoft staff and others that have met me know I look nothing like this AVATAR picture. ;-)
    • Edited by John Anthony Oliver Wednesday, June 11, 2008 10:10 AM Added text.
    • Marked as answer by Dora9 Friday, June 13, 2008 8:59 AM
    • Marked as answer by Dora9 Friday, June 13, 2008 8:59 AM
    • Marked as answer by Dora9 Friday, June 13, 2008 8:59 AM
    • Marked as answer by Dora9 Friday, June 13, 2008 8:59 AM
    • Marked as answer by Dora9 Friday, June 13, 2008 8:59 AM
    Wednesday, June 11, 2008 9:53 AM
  • Hi,

    I have don it with code (got to hate VB when it comes to messing around with int's) wich i had leant't C++ or C# although both should be easy to pick up as i code in C every day.

    Here is the solution : -

      Dim i As Integer = 0

            OpenDialog.Title = "Load File"
            OpenDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
            OpenDialog.FileName = ""
            OpenDialog.FilterIndex = 1

            OpenDialog.InitialDirectory = "MyDocuments"

            OpenDialog.CheckFileExists = True
            OpenDialog.CheckPathExists = True
            Try
                If OpenDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then

                    Dim myStreamReader As New StreamReader(OpenDialog.FileName)

                    Do While myStreamReader.Peek() >= 0

                        Dim temp As Byte = 0
                        Dim temp2 As Byte = 0

                        temp = myStreamReader.Read

                        If temp >= 48 And temp <= 56 Then

                            temp = temp - 48

                        Else

                            temp = temp - 55

                        End If

                        temp2 = myStreamReader.Read

                        If temp2 >= 48 And temp2 <= 56 Then

                            temp2 = temp2 - 48

                        Else

                            temp2 = temp2 - 55

                        End If

                        temp = ((temp * 16) + temp2)


                        mReadBytes.Add(temp)

                    Loop

                    myStreamReader.Close()

                End If

            Catch ex As Exception

                MsgBox(ex.Message.ToString, MsgBoxStyle.Information, "Error")

            End Try

    Sorry the display as code thingy in the form has broken for the night or atleast it has on my computer (using firefox  may be the problem but it is much better than IE - IMO)

    there is probably a windows function that does the above but hey it works

    Andy

    Student
    • Marked as answer by Dora9 Tuesday, June 10, 2008 10:52 PM
    Tuesday, June 10, 2008 10:52 PM

All replies

  • There may be a better way, but this'll work.  You'll need to modify it a little if you want to retrun the answer in some form other than a string.

    Sub Main()  
        Console.WriteLine(BCD(1234))  
        Console.WriteLine("")  
        Console.WriteLine("Press any key to end")  
        Console.ReadKey()  
    End Sub  
     
    Public Function BCD(ByVal Value As Integer) As String  
     
        Select Case Value  
        Case Is < 0 : Return "Error - negative value"  
        Case Is < 16 : Return Chr(Value)  
        End Select  
     
        Dim sBCDValue As String = "" 
        Dim iTemp As Integer = 0 
     
        Do Until Value = 0 
            iTemp = Value Mod 100  
            Value \= 100  
            sBCDValue = Chr(((iTemp \ 10) * 16) + (iTemp Mod 10)) & sBCDValue  
        Loop  
     
        Return sBCDValue  
    End Function 
    Tuesday, June 10, 2008 12:41 PM
  • Here is  a work around -- it works for me !!

    Imports System.IO
    Public Class Form1
        Dim pos As Integer = 0
        Dim hexVal As String
        Dim myCh As String = String.Empty
        Dim s As String

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Using br As BinaryReader = New BinaryReader(New FileStream("C:\MEC\tst.txt", FileMode.Open))
            Using sr As StreamReader = New StreamReader("C:\MEC\tst.txt")
                s = sr.ReadToEnd

                For Each ch As Char In s
                    pos += 1
                    myCh = myCh + ch
                    If pos Mod 2 = 0 Then
                        hexVal = Hex(myCh) ' do what you want with the HEX value stored in hexVal
                        myCh = String.Empty
                    End If
                Next


                'hexVal = Hex(byteRead)
            End Using
        End Sub

    End Class

    Tuesday, June 10, 2008 1:44 PM
  • Hi
    Keep in mind that :
    e.g.: Hex(10) = A
    and Hex (20) = 14
    So you have to use "x = x & right("0" & Hex(twodigitsnumber),2)" each pair :)
    HTH
    Guy
    TIA Guy Cohen
    Tuesday, June 10, 2008 3:06 PM
  • Hi,

    I have don it with code (got to hate VB when it comes to messing around with int's) wich i had leant't C++ or C# although both should be easy to pick up as i code in C every day.

    Here is the solution : -

      Dim i As Integer = 0

            OpenDialog.Title = "Load File"
            OpenDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
            OpenDialog.FileName = ""
            OpenDialog.FilterIndex = 1

            OpenDialog.InitialDirectory = "MyDocuments"

            OpenDialog.CheckFileExists = True
            OpenDialog.CheckPathExists = True
            Try
                If OpenDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then

                    Dim myStreamReader As New StreamReader(OpenDialog.FileName)

                    Do While myStreamReader.Peek() >= 0

                        Dim temp As Byte = 0
                        Dim temp2 As Byte = 0

                        temp = myStreamReader.Read

                        If temp >= 48 And temp <= 56 Then

                            temp = temp - 48

                        Else

                            temp = temp - 55

                        End If

                        temp2 = myStreamReader.Read

                        If temp2 >= 48 And temp2 <= 56 Then

                            temp2 = temp2 - 48

                        Else

                            temp2 = temp2 - 55

                        End If

                        temp = ((temp * 16) + temp2)


                        mReadBytes.Add(temp)

                    Loop

                    myStreamReader.Close()

                End If

            Catch ex As Exception

                MsgBox(ex.Message.ToString, MsgBoxStyle.Information, "Error")

            End Try

    Sorry the display as code thingy in the form has broken for the night or atleast it has on my computer (using firefox  may be the problem but it is much better than IE - IMO)

    there is probably a windows function that does the above but hey it works

    Andy

    Student
    • Marked as answer by Dora9 Tuesday, June 10, 2008 10:52 PM
    Tuesday, June 10, 2008 10:52 PM


  • Hi Andy,

    9 has the ASCII code of 57 so you should change these two lines.>>

       If temp >= 48 And temp <= 56 Then

      If temp2 >= 48 And temp2 <= 56 Then


    to have 57 instead of 56.

    For proof of this see this internet web page.>>





    Regards,

    John



    My profile picture IS NOT really me. I do not have any facial tattoos or piercings in real life. The Microsoft staff and others that have met me know I look nothing like this AVATAR picture. ;-)
    • Edited by John Anthony Oliver Wednesday, June 11, 2008 8:42 AM Removed quoted text.
    • Marked as answer by Dora9 Wednesday, June 11, 2008 9:18 PM
    Wednesday, June 11, 2008 8:41 AM

  • 1Option Strict On 
    2Imports System.IO 
    3 
    4Public Class Form1 
    5 
    6Private mReadBytes As New List(Of Byte) 
    7 
    8Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    9 
    10Dim ofd As New OpenFileDialog 
    11        ofd.Title = "Load File" 
    12        ofd.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*" 
    13        ofd.FileName = "" 
    14        ofd.FilterIndex = 1 
    15        ofd.InitialDirectory = "MyDocuments" 
    16        ofd.CheckFileExists = True 
    17        ofd.CheckPathExists = True 
    18        Try 
    19            If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then 
    20 
    21                Dim myStreamReader As New StreamReader(ofd.FileName) 
    22     
    23                Do While myStreamReader.EndOfStream = False 
    24 
    25                    Dim temp As String 
    26                    Dim temp2 As String 
    27                    temp = CStr(myStreamReader.Read) 
    28                    temp2 = CStr(myStreamReader.Read) 
    29                    Dim total As Integer = Val(Chr(CInt(Val(temp)))) * 16 
    30                    total += Val(Chr(CInt(Val(temp2)))) 
    31                    mReadBytes.Add(Convert.ToByte(total)) 
    32 
    33                Loop 
    34                myStreamReader.Close() 
    35            End If 
    36 
    37        Catch ex As Exception 
    38            MsgBox(ex.Message.ToString, MsgBoxStyle.Information, "Error") 
    39        End Try 
    40 
    41'This section will show the HEX as ASCII text.>
    42Dim myArray() As Byte = mReadBytes.ToArray 
    43Dim output As String = "" 
    44For num As Integer = 0 To myArray.GetUpperBound(0) 
    45output &= Chr(Convert.ToInt32(mReadBytes(num))) 
    46Next 
    47MessageBox.Show(output, "Result....") 
    48 
    49End Sub 
    50End Class 
    51 
    52 

    Hi Andy,

    In the above code I am not using any IF THEN statements other than.>>

     If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then



    I still multiply by 16 to get the HighByte then add the LowByte in the same way.

    So if the text file contains 41 42 like this.>>

    4142

    this translates to 65 and 66 respectively in base 10.

    These are the characters AB in text format from the ASCII character table at.>>





    Regards,

    John


    My profile picture IS NOT really me. I do not have any facial tattoos or piercings in real life. The Microsoft staff and others that have met me know I look nothing like this AVATAR picture. ;-)
    • Edited by John Anthony Oliver Wednesday, June 11, 2008 10:10 AM Added text.
    • Marked as answer by Dora9 Friday, June 13, 2008 8:59 AM
    • Marked as answer by Dora9 Friday, June 13, 2008 8:59 AM
    • Marked as answer by Dora9 Friday, June 13, 2008 8:59 AM
    • Marked as answer by Dora9 Friday, June 13, 2008 8:59 AM
    • Marked as answer by Dora9 Friday, June 13, 2008 8:59 AM
    Wednesday, June 11, 2008 9:53 AM
  • Hi john,

    I wish i had read you post regarding my stupid misktake : -

    9 has the ASCII code of 57 so you should change these two lines.>>


       If temp >= 48 And temp <= 56 Then

    ** Should be
    If temp >= 48 And temp <= 57 Then

      If temp2 >= 48 And temp2 <= 56 Then




    Beasuse i have just spent 4hrs searching around the code for a possible answers , and finally found it then logged on and read your post (then started kicking myself), i wish microsoft would stop messing with the forum and put it back the same way as before so it all actually works. then i would have recived the e-mail telling me that you had posted a response.

    I have had a look at your code, it looks a lot clearner so i may use it instead. although not as portable / readable as in it would be difficult for somebody (non VB.net) to port the code onto another VB comilpler as it uses lot's of microsoft libaries instead of simple maths.

    BTW i a have it all working now :)

    EDIT; Is it possible to make the form i have this in, always on top, and stop the user moving the form while the information is sent to the external device?

    somthing like form1.move = false or somthing similar

    Thanks all for your help,

    Andy



    Student
    • Edited by Dora9 Wednesday, June 11, 2008 11:59 PM Edit
    Wednesday, June 11, 2008 9:18 PM
  • xplosiv_1 said:

    Hi John,


    EDIT; Is it possible to make the form i have this in, always on top, and stop the user moving the form while the information is sent to the external device?

    somthing like form1.move = false or somthing similar

    Thanks all for your help,

    Andy



    Student

    Hi Andy,

    Best that I know is to do MAXIMISE the FORM and turn the CONTROLBOX section off, see below.
    If you do that though add a button to Exit the Application using Application.Exit()

    By the way when you add a post you can now tick the CheckBox just above SUBMIT if you want alerts to your email inbox you have to Subscribe to Alerts for this thread.


    Regards,

    John





    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'You could set this property in the FORM properties window.>>
    Me.WindowState = FormWindowState.Maximized
    'You could set this property in the FORM properties window.>>
    Me.ControlBox = False

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'If you ever do a multi-FORM application
    'this will EXIT the application from ANY FORM
    'so it is better than Me.Close.>>
    Application.Exit()

    End Sub
    End Class


    My profile picture IS NOT really me. I do not have any facial tattoos or piercings in real life. The Microsoft staff and others that have met me know I look nothing like my AVATAR picture. ;-)
    Thursday, June 12, 2008 10:21 PM
  • Hi John,

    Many Thanks for your help.

    Andy

    Student
    Friday, June 13, 2008 8:58 AM