System.Byte() array convert to string... HOW???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jarrod Sharp

    #1

    System.Byte() array convert to string... HOW???

    Using the VB6 Winsock control in VB.NET the GetData(byRef Data as Object) method of the control returns an Object.

    I have used this code that works but am not familiar with .NET arrays/object/conversions...

    Private Sub wskSocketPlus_D ataArrival(ByVa l sender As Object, ByVal e As AxMSWinsockLib. DMSWinsockContr olEvents_DataAr rivalEvent) Handles wskSocketPlus.D ataArrival

    Dim o As Object
    wskSocketPlus.G etData(o)
    Dim incomingDataArr ay() As Byte
    incomingDataArr ay = CType(o, System.Byte())

    Dim Buffer As New StringBuilder
    Dim b As Byte

    For element As Integer = 0 To incomingDataArr ay.Length - 1
    b = Convert.ToByte( incomingDataArr ay.GetValue(ele ment))
    Buffer.Append(C hr(b))
    Next element

    MessageBox.Show (Buffer.ToStrin g)

    End Sub

    I was told I should use string builder as it is fatser than concatination. The datapackets are around a few thosand bytes. Any suggestions as to a more correct set of conversions to give me a resulting string of the incoming Object data?

    thanks, Jarrod


  • Jarrod Sharp

    #2
    RE: System.Byte() array convert to string... HOW???

    this worked as well.... but geez.... why no GetData(string) ??

    Dim o As Object
    AxWinsock2.GetD ata(o)

    Dim b() As Byte
    b = CType(o, Byte())

    Dim a As ASCIIEncoding
    a = New ASCIIEncoding

    Dim s As String
    s = a.GetString(b)

    MessageBox.Show (s)

    Comment

    • Jon Skeet [C# MVP]

      #3
      RE: System.Byte() array convert to string... HOW???

      Jarrod Sharp <JarrodSharp@di scussions.micro soft.com> wrote:[color=blue]
      > this worked as well.... but geez.... why no GetData(string) ??[/color]

      Because a socket is a fundamentally binary object. I think it's great
      that the framework makes such a clear distinction between binary data
      and character data.

      Note that you don't need to create a new instance of ASCIIEncoding
      yourself - just use the Encoding.ASCII property.

      (I'd also suggest using the Socket class that's built into the
      framework, btw - or even TcpClient.)

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      Working...