[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?
Re: ASCII to HEX buffer converting
Re: ASCII to HEX buffer converting
Why do you do this?
VB.NET Code:
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:
rest = rest & "&h" & inputReportBuffer_element.ToString("X2")
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..
Re: ASCII to HEX buffer converting
Just do something like:
VB.NET Code:
For Each aski As Byte In myArr
TextBox1.Text &= Convert.ToString(aski, 16) & " "
Next
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
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:
rest & Chr(Val(inputReportBuffer_element.ToString("X2"))) & " "
Re: [RESOLVED] ASCII to HEX buffer converting
Quote:
Originally Posted by
dr_aybyd
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