Encoding Query

Jay1b

Contributor
Joined
Aug 3, 2003
Messages
640
Location
Kent, Uk.
Hi

For those of you that saw the other thread, i've managed to find out why it wasnt working and find a work around, but i dont know why one works and not the other. Could someone have a look and tell me what they think please?

This is the original code. It was largely working fine, but the first char was chr(153) or Hex value 99. It would convert this into a ?, hex value 3F or Chr(63). I'm guessing it was reading the first character from the string as a ? as its an unprintable symbol, and then outputting this to an actual ? in the conversion.

Visual Basic:
            mobjClient = New TcpClient("172.28.46.27", 3500)
  
            Dim b() As Byte = System.Text.Encoding.ASCII.GetBytes(sMessage)
   
            Dim s As IO.Stream = mobjClient.GetStream
            s.Write(b, 0, b.Length)
            s.Flush()
            s.Close()

However I tried the change below (ok so my boss did, who has never used .Net before) and almost annoyingly it worked.

Visual Basic:
            mobjClient = New TcpClient("172.28.46.27", 3500)
  
            Dim b(sMessage.Length - 1) As Byte
            For i As Integer = 0 To sMessage.Length - 1
                b(i) = Asc(sMessage.Substring(i, 1))
            Next

            Dim s As IO.Stream = mobjClient.GetStream
            s.Write(b, 0, b.Length)
            s.Flush()
            s.Close()

Can someone please tell me why the first piece of code didnt work but the second did? Why could it not convert it correctly as part of a string, but it could byte by byte.

Obviously its working, which i'm happy about, but i would like to know why if anyone can help please?

Thanks
 
Yeah, that is definitely weird. Are you sure that everything else is identical besides the code you have listed?
 
Yes its the same piece of code, i just comment out either
Visual Basic:
Dim b() As Byte = System.Text.Encoding.ASCII.GetBytes(sMessage)

or

Visual Basic:
            Dim b(sMessage.Length - 1) As Byte
            For i As Integer = 0 To sMessage.Length - 1
                b(i) = Asc(sMessage.Substring(i, 1))
            Next
 
Its a string compiled below.

Visual Basic:
    Public sHeader As String 'Holds Message Header
    Public sNumMessage As String 'Holds No. of Messages
    Public sNumDestination As String 'Holds No. of Destinations
    Public sNumSigns As String 'Holds Sign Numbers
    Public sMessHigh As String 'Holds Message High
    Public sMessLow As String 'Holds Message Low
    Public sMessText1 As String 'Holds Message Text for line 1
    Public sMessText2 As String 'Holds message text for line 2
    Public sMessRolling As String 'Holds rolling string
    Public sMessLargeRolling As String
    Public bytChecksum As Byte 'Holds Message Checksum
    Public Shared sMessage As String 'String to hold all of the above
    Public sEsc As String 'Holds Escape ASCII Character
    Public sForeColorStr As String 'holds foreground colour of text

    Private Sub ................................
        'set escape string
        sEsc = Chr(&H1BS)

        bSocketInUse = False
        ' initialise to red text on black background
        sForeColorStr = Chr(27) & "[31;40m"

        Dim strMessage As String = ""
        Dim strEsc As String = Chr(&H1BS) 'Holds Escape ASCII Character
        sHeader = Chr(&H99S)
        sNumMessage = Chr(&H1S)
        sNumDestination = Chr(&H1S)
        sNumSigns = Chr(&H1S)
        sMessHigh = Chr(0)

        sMessText1 = strEsc & "[2J" & strEsc & "[1;1H" & sForeColorStr & "0937"

        sMessLow = Chr(Len(Trim(sMessText1)) + Len(Trim(sMessText2)))

        'build message for transmission
        sMessage = sHeader & sNumMessage & sNumDestination & sNumSigns & sMessLow & sMessHigh ' this is the message header stuff
        sMessage = sMessage & sMessText1  'add text for top line ' & sEsc & "[4x"

        bytChecksum = 0
        ' update checksum using each byte of the message
        For i As Integer = 1 To Len(sMessage)
            bytChecksum = RotateLeft(bytChecksum)
            bytChecksum = bytChecksum Xor Asc(Mid(sMessage, i, 1))
        Next
        ' do one further rotation
        bytChecksum = RotateLeft(bytChecksum)
        ' and append the checksum to the message
        sMessage = sMessage & Chr(bytChecksum)
 
Back
Top