Results 1 to 4 of 4

Thread: [2005] {Resolved} Chr(Int) not working as expected

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    102

    [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:
    1. Public Class clsPacket
    2.  
    3.     Private _Data As New List(Of Byte)
    4.  
    5.     Public ReadOnly Property Size() As Integer
    6.         Get
    7.             Return _Data.Count - 1
    8.         End Get
    9.     End Property
    10.  
    11.     Public ReadOnly Property GetPoint(ByVal Index As Integer) As Byte
    12.         Get
    13.             Return _Data.Item(Index)
    14.         End Get
    15.     End Property
    16.  
    17.     Public ReadOnly Property GetHeader() As Byte
    18.         Get
    19.             Return _Data.Item(0)
    20.         End Get
    21.     End Property
    22.  
    23.     Public ReadOnly Property GetPacket() As Byte()
    24.         Get
    25.             _Data.TrimExcess()
    26.             Dim TempByte(_Data.Count - 1) As Byte
    27.             For TempInt As Integer = 0 To _Data.Count - 1
    28.                 TempByte(TempInt) = _Data.Item(TempInt)
    29.             Next
    30.             Return TempByte
    31.         End Get
    32.     End Property
    33.  
    34.  Public Sub RemovePortion(ByVal Amount As Integer)
    35.         _Data.RemoveRange(0, Amount - 1)
    36.         _Data.TrimExcess()
    37.     End Sub
    38.  
    39.     Public Sub ClearPacket()
    40.         _Data.Clear()
    41.     End Sub
    42.  
    43.     Public Sub DumpPacket(ByVal NewData() As Byte)
    44.         _Data.Clear()
    45.         For TempInt As Integer = 0 To NewData.Length - 1
    46.             _Data.Add(NewData(TempInt))
    47.         Next
    48.     End Sub
    49.  
    50.         Public Function ReadASCIIStringUntilNull() As String
    51.         Dim TempString As String = "", TempInt As Integer
    52.         For TempInt = 0 To Size
    53.             If _Data.Item(TempInt) <> 0 Then
    54.                 TempString = TempString & Chr(_Data.Item(TempInt))
    55.             Else
    56.                 Exit For
    57.             End If
    58.         Next TempInt
    59.         RemovePortion(TempInt)
    60.         Return TempString
    61.     End Function
    62.  
    63.     Public Sub WriteASCIINull(ByVal TempString As String)
    64.         For TempInt As Integer = 0 To Len(TempString) - 1
    65.             _Data.Add(Asc(TempString))
    66.             TempString = Right(TempString, Len(TempString) - 1)
    67.         Next TempInt
    68.         _Data.Add(&H0)
    69.     End Sub
    70. End Class


    Main Form
    vb Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim test As New clsPacket
    3.         test.WriteASCIINull("test")
    4.         test.WriteASCIINull("test2")
    5.         Dim Log As String
    6.         Log = vbCrLf & "Packet (0x" & Hex(test.GetPoint(0)) & ")" & vbCrLf
    7.         Log += "Time/Date - " & TimeOfDay() & " / " & Now.Date() & vbCrLf
    8.         Log += "Packet - "
    9.         For i As Integer = 0 To test.Size
    10.             Log += Hex(test.GetPoint(i)) & " "
    11.         Next i
    12.         Log += vbCrLf & "ASCII - "
    13.         For i As Integer = 0 To test.Size
    14.             Log += Chr(test.GetPoint(i)) & " "
    15.         Next
    16.         Log += vbCrLf & "Length - " & test.Size & vbCrLf
    17.         MsgBox(Log)
    18.         MsgBox(test.ReadASCIIStringUntilNull & " " & test.ReadASCIIStringUntilNull)
    19.     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.

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