Results 1 to 4 of 4

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

  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.

  2. #2
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    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 ...

  3. #3
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    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.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    102

    Re: [2005] Chr(Int) not working as expected

    Thank you it worked.!!!
    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
  •  



Click Here to Expand Forum to Full Width