Results 1 to 8 of 8

Thread: [RESOLVED] ASCII to HEX buffer converting

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Resolved [RESOLVED] ASCII to HEX buffer converting

    Hi I found this function from the net.
    But I have a problem with the reply buffer.
    The reply buffer is in ASCII value...but I need is HEX value.
    Is there any way that the reply buffer is in hex?

    Code:
    Public Function read_write_string(ByVal devicehandle, ByVal command, ByVal commandlenght) As String
            Dim inputReportBuffer(100) As Byte
            unManagedBuffer = Marshal.AllocHGlobal(inputReportBuffer.Length)
            numberOfBytesRead = 0
            numberOfBytesWritten = 0
    
            success = WriteFile _
            (devicehandle, _
            command, _
            commandlenght, _
            numberOfBytesWritten, _
            IntPtr.Zero)
            Thread.Sleep(50)
    
            success = ReadFile _
               (devicehandle, _
                unManagedBuffer, _
                inputReportBuffer.Length, _
                numberOfBytesRead, _
                unManagedOverlapped)
    
            Marshal.Copy _
            (unManagedBuffer, inputReportBuffer, 0, numberOfBytesRead)
    
            ReDim Preserve inputReportBuffer(numberOfBytesRead - 1)
    
            Dim inputReportBuffer_element As Byte
    
            Dim rest As String = ""
            Dim n As Integer = 0
    
    
            For Each inputReportBuffer_element In inputReportBuffer
                If n > commandlenght - 1 Then
                    Select Case inputReportBuffer_element
    
                        Case Is < &H20
                            rest = rest & " "
                        Case Is = &H20
                            rest = rest & " "
                        Case Is > &H20
    
                            rest = rest & Chr(Val("&h" & inputReportBuffer_element.ToString("X2")))
                    End Select
                End If
                n += 1
            Next
    
            Return rest.Trim
        End Function
    This is my codes that I am sending to device via usb

    Code:
        Private Sub AxLaVolpeButton6_ClickEvent(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AxLaVolpeButton6.ClickEvent
            Dim sp_f() As Byte = {&H1B, &H0, &H10, &H53, &H0, &H5, &H0, &HF, &H16, &H12, &HD}
            TextBox1.Text = read_write_str(deviceHandle, sp_f, sp_f.Length)
        End Sub
    My expected answer is:
    Code:
    1B 10 00 53 00 5C 0F 40 16 13 12 55 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 00 44 80 00 00 00 00 00 00 06 24 40 70 00 00 00 00 00 00 18 07 00 00 00 00 00 00 50 00 00 05 FF FF FF 00 B4 00 00 05 FF FF FF 01 18 00 00 05 FF FF FF 01 7C 00 00 05 FF FF FF 01 E0 00 00 05 FF FF FF 02 44 00 00
    but in my current codes the answer is:
    Code:
          Uÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ D€       $@p              P   ÿÿÿ ´   ÿÿÿ     ÿÿÿ |   ÿÿÿ à   ÿÿÿ D
    which is not i need.

    Anyone can help me?
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Re: ASCII to HEX buffer converting

    Up for my thread!!!
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

  3. #3
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: ASCII to HEX buffer converting

    Why do you do this?

    VB.NET Code:
    1. rest & Chr(Val("&h" & inputReportBuffer_element.ToString("X2")))

    You get the Hex value of the buffer, then you get the value (double with Val), and finally get the char value... If you expect the hex value just use this part...

    VB.NET Code:
    1. rest = rest & "&h" & inputReportBuffer_element.ToString("X2")

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Re: ASCII to HEX buffer converting

    I mildly got it now
    by this codes

    Code:
     Private Sub AxLaVolpeButton6_ClickEvent(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AxLaVolpeButton6.ClickEvent
            Dim sp_f() As Byte = {&H1B, &H0, &H10, &H53, &H0, &H5, &H0, &HF, &H16, &H12, &HD}
            Dim myString As String = read_write_str(deviceHandle, sp_f, sp_f.Length)
    
            Dim myEncoding As New System.Text.ASCIIEncoding
            Dim myArr() As Byte = myEncoding.GetBytes(myString)
            For Each aski As Byte In myArr
                ListBox2.Items.Add(Convert.ToString(aski, 16))
            Next
        End Sub
    My question now is how can I use Textbox instead of ListBox?
    To appear like this

    Code:
    1B 10 00 53 00 5C 0F 40 16 13 12 55 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 00 44 80 00 00 00 00 00 00 06 24 40 70 00 00 00 00 00 00 18 07 00 00 00 00 00 00 50 00 00 05 FF FF FF 00 B4 00 00 05 FF FF FF 01 18 00 00 05 FF FF FF 01 7C 00 00 05 FF FF FF 01 E0 00 00 05 FF FF FF 02 44 00 00
    and one thing I noticed also is the FF FF is appear something like 3f 3f


    @mickey_pt

    thats for my other use..
    Last edited by dr_aybyd; Sep 16th, 2009 at 04:53 AM.
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

  5. #5
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: ASCII to HEX buffer converting

    Just do something like:

    VB.NET Code:
    1. For Each aski As Byte In myArr
    2.    TextBox1.Text &= Convert.ToString(aski, 16) & " "
    3. Next

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Re: ASCII to HEX buffer converting

    Ouch i noticed another error:
    The output is like this:

    Code:
    55 3f 3f 3f 3f 3f 3f 3f 3f 3f 3f 3f 3f 3f 3f 3f 3f 20 44 3f 20 20 20 20 20 20 20 24 40 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 20 20 20 3f 3f 3f 20 3f 20 20 20 3f 3f 3f 20 20 20 20 20 3f 3f 3f 20 7c 20 20 20 3f 3f 3f 20 3f 20 20 20 3f 3f 3f 20 44
    My expected output must be like this:

    Code:
    1B 10 00 53 00 5C 0F 40 16 13 12 55 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 00 44 80 00 00 00 00 00 00 06 24 40 70 00 00 00 00 00 00 18 07 00 00 00 00 00 00 50 00 00 05 FF FF FF 00 B4 00 00 05 FF FF FF 01 18 00 00 05 FF FF FF 01 7C 00 00 05 FF FF FF 01 E0 00 00 05 FF FF FF 02 44 00 00
    @mickey

    when i use your codes:
    Code:
    rest & Chr(Val("&h" & inputReportBuffer_element.ToString("X2")))

    The replied buffer is like this:

    Code:
    &h55&hFF&hFF&hFF&hFF&hFF&hFF&hFF&hFF&hFF&hFF&hFF&hFF&hFF&hFF&hFF&hFF &h44&h80       &h24&h40&h70              &h50   &hFF&hFF&hFF &hB4   &hFF&hFF&hFF     &hFF&hFF&hFF &h7C   &hFF&hFF&hFF &hE0   &hFF&hFF&hFF &h44
    Last edited by dr_aybyd; Sep 16th, 2009 at 05:52 AM.
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

  7. #7
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: ASCII to HEX buffer converting

    In the different values obtained in the answer i think i can't help you, i don't know what answer to expect and i don't know what you're writing and reading...

    To correct the reply buffer...
    VB.NET Code:
    1. rest & Chr(Val(inputReportBuffer_element.ToString("X2"))) & " "

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  8. #8
    New Member
    Join Date
    Jan 2011
    Posts
    12

    Re: [RESOLVED] ASCII to HEX buffer converting

    Quote Originally Posted by dr_aybyd View Post
    Hi I found this function from the net.
    But I have a problem with the reply buffer.
    The reply buffer is in ASCII value...but I need is HEX value.
    Is there any way that the reply buffer is in hex?

    Code:
    Public Function read_write_string(ByVal devicehandle, ByVal command, ByVal commandlenght) As String
            Dim inputReportBuffer(100) As Byte
            unManagedBuffer = Marshal.AllocHGlobal(inputReportBuffer.Length)
            numberOfBytesRead = 0
            numberOfBytesWritten = 0
    
            success = WriteFile _
            (devicehandle, _
            command, _
            commandlenght, _
            numberOfBytesWritten, _
            IntPtr.Zero)
            Thread.Sleep(50)
    
            success = ReadFile _
               (devicehandle, _
                unManagedBuffer, _
                inputReportBuffer.Length, _
                numberOfBytesRead, _
                unManagedOverlapped)
    
            Marshal.Copy _
            (unManagedBuffer, inputReportBuffer, 0, numberOfBytesRead)
    
            ReDim Preserve inputReportBuffer(numberOfBytesRead - 1)
    
            Dim inputReportBuffer_element As Byte
    
            Dim rest As String = ""
            Dim n As Integer = 0
    
    
            For Each inputReportBuffer_element In inputReportBuffer
                If n > commandlenght - 1 Then
                    Select Case inputReportBuffer_element
    
                        Case Is < &H20
                            rest = rest & " "
                        Case Is = &H20
                            rest = rest & " "
                        Case Is > &H20
    
                            rest = rest & Chr(Val("&h" & inputReportBuffer_element.ToString("X2")))
                    End Select
                End If
                n += 1
            Next
    
            Return rest.Trim
        End Function
    This is my codes that I am sending to device via usb

    Code:
        Private Sub AxLaVolpeButton6_ClickEvent(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AxLaVolpeButton6.ClickEvent
            Dim sp_f() As Byte = {&H1B, &H0, &H10, &H53, &H0, &H5, &H0, &HF, &H16, &H12, &HD}
            TextBox1.Text = read_write_str(deviceHandle, sp_f, sp_f.Length)
        End Sub
    My expected answer is:
    Code:
    1B 10 00 53 00 5C 0F 40 16 13 12 55 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 00 44 80 00 00 00 00 00 00 06 24 40 70 00 00 00 00 00 00 18 07 00 00 00 00 00 00 50 00 00 05 FF FF FF 00 B4 00 00 05 FF FF FF 01 18 00 00 05 FF FF FF 01 7C 00 00 05 FF FF FF 01 E0 00 00 05 FF FF FF 02 44 00 00
    but in my current codes the answer is:
    Code:
          Uÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ D€       $@p              P   ÿÿÿ ´   ÿÿÿ     ÿÿÿ |   ÿÿÿ à   ÿÿÿ D
    which is not i need.

    Anyone can help me?
    Hi, I am too stuck at the same place. If you have done this please help me too.

    BR

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