|
-
Jun 16th, 2007, 04:02 AM
#1
Thread Starter
Lively Member
[2005] {Resolved} Chr(Int) not working as expected
Hi All,
I got a strange one im trying to sort out. I have a class called clsPacket which writes to a byte array in various formats (string, int etc) and Im coming up against a strange problem. I've used simlair code before and not recieved this problem and it is confusing me quite a bit now.
The problem I am getting is I write two strings to the array and try and then to test they are correct (this is a test app I made) I first look at the Log I created and then use the built in packet functions I made to display the results in a message box. They return two different results which are both wrong. I'll include all the necersary code for if someone wants to see this for themselves.
clsPacket - Packet Class
vb Code:
Public Class clsPacket
Private _Data As New List(Of Byte)
Public ReadOnly Property Size() As Integer
Get
Return _Data.Count - 1
End Get
End Property
Public ReadOnly Property GetPoint(ByVal Index As Integer) As Byte
Get
Return _Data.Item(Index)
End Get
End Property
Public ReadOnly Property GetHeader() As Byte
Get
Return _Data.Item(0)
End Get
End Property
Public ReadOnly Property GetPacket() As Byte()
Get
_Data.TrimExcess()
Dim TempByte(_Data.Count - 1) As Byte
For TempInt As Integer = 0 To _Data.Count - 1
TempByte(TempInt) = _Data.Item(TempInt)
Next
Return TempByte
End Get
End Property
Public Sub RemovePortion(ByVal Amount As Integer)
_Data.RemoveRange(0, Amount - 1)
_Data.TrimExcess()
End Sub
Public Sub ClearPacket()
_Data.Clear()
End Sub
Public Sub DumpPacket(ByVal NewData() As Byte)
_Data.Clear()
For TempInt As Integer = 0 To NewData.Length - 1
_Data.Add(NewData(TempInt))
Next
End Sub
Public Function ReadASCIIStringUntilNull() As String
Dim TempString As String = "", TempInt As Integer
For TempInt = 0 To Size
If _Data.Item(TempInt) <> 0 Then
TempString = TempString & Chr(_Data.Item(TempInt))
Else
Exit For
End If
Next TempInt
RemovePortion(TempInt)
Return TempString
End Function
Public Sub WriteASCIINull(ByVal TempString As String)
For TempInt As Integer = 0 To Len(TempString) - 1
_Data.Add(Asc(TempString))
TempString = Right(TempString, Len(TempString) - 1)
Next TempInt
_Data.Add(&H0)
End Sub
End Class
Main Form
vb Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim test As New clsPacket
test.WriteASCIINull("test")
test.WriteASCIINull("test2")
Dim Log As String
Log = vbCrLf & "Packet (0x" & Hex(test.GetPoint(0)) & ")" & vbCrLf
Log += "Time/Date - " & TimeOfDay() & " / " & Now.Date() & vbCrLf
Log += "Packet - "
For i As Integer = 0 To test.Size
Log += Hex(test.GetPoint(i)) & " "
Next i
Log += vbCrLf & "ASCII - "
For i As Integer = 0 To test.Size
Log += Chr(test.GetPoint(i)) & " "
Next
Log += vbCrLf & "Length - " & test.Size & vbCrLf
MsgBox(Log)
MsgBox(test.ReadASCIIStringUntilNull & " " & test.ReadASCIIStringUntilNull)
End Sub
If you run the code you will see that from the first message box you get to see the raw packet which shows the words (in their ascii codes). but underneath you only see one word. Then on the second messagebox which uses the packets own function to show the words, you see one word, a space then the first letter of the second word. I have no clue why this is happening, any ideas?
Last edited by Dilvid; Jun 17th, 2007 at 04:59 PM.
-
Jun 16th, 2007, 11:42 AM
#2
Fanatic Member
Re: [2005] Chr(Int) not working as expected
Since you seem to be manipulating bytes into ASCII strings have you tried reading your input using a BinaryReader and then passing on the bytes to various methods exposed by the Convert class.
Using VB.NET 2003/.NET 1.1/C# 2.0
http://del.icio.us/rajoo
Blow your mind, smoke gunpowder
Ashes to ashes, dust to dust
If God won't have you, the devil will. - Author unknown
Don't follow me, I'm lost too ...
-
Jun 16th, 2007, 12:34 PM
#3
Re: [2005] Chr(Int) not working as expected
The first message box fails because as soon as your loop finds a zero-byte, it still appends it to your string. When the string is then displayed in the messagebox, it only displays up until this byte and no further. Change the loop to this to display a space in place of the zero-byte:
Code:
For i As Integer = 0 To test.Size
If Not test.GetPoint(i) = 0 Then
Log &= Convert.ToChar(test.GetPoint(i)) & " "
Else
Log &= " "
End If
Next i
The second messagebox fails because you are not removing the correct amount of bytes from your list. Change the RemovePortion sub to this:
Code:
Public Sub RemovePortion(ByVal Amount As Integer)
_Data.RemoveRange(0, Amount + 1)
_Data.TrimExcess()
End Sub
Also, while having a look at this, it was clear that you have not set Option Strict On, and this threw up a few warnings. I took the liberty to change the following code to fix it, and to do a couple of things in a more '.NET' way:
Code:
Public Function ReadASCIIStringUntilNull() As String
Dim TempString As String = "", TempInt As Integer
For TempInt = 0 To Size
If _Data.Item(TempInt) <> 0 Then
TempString &= Convert.ToChar(_Data.Item(TempInt))
Else
Exit For
End If
Next TempInt
RemovePortion(TempInt)
Return TempString
End Function
Public Sub WriteASCIINull(ByVal TempString As String)
For TempInt As Integer = 0 To TempString.Length - 1
Dim c As Char = Convert.ToChar(TempString.Substring(TempInt, 1))
Dim b As Byte = Convert.ToByte(c)
_Data.Add(b)
Next TempInt
Data.Add(&H0)
End Sub
Hope that helps and does something like you want it to do.
-
Jun 17th, 2007, 04:55 PM
#4
Thread Starter
Lively Member
Re: [2005] Chr(Int) not working as expected
Last edited by Dilvid; Jun 17th, 2007 at 04:58 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|