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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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_DataArrival(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles wskSocketPlus.DataArrival

Dim o As Object
wskSocketPlus.GetData(o)
Dim incomingDataArray() As Byte
incomingDataArray = CType(o, System.Byte())

Dim Buffer As New StringBuilder
Dim b As Byte

For element As Integer = 0 To incomingDataArray.Length - 1
b = Convert.ToByte(incomingDataArray.GetValue(element))
Buffer.Append(Chr(b))
Next element

MessageBox.Show(Buffer.ToString)

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
 
this worked as well.... but geez.... why no GetData(string) ??

Dim o As Object
AxWinsock2.GetData(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)
 
Jarrod Sharp said:
this worked as well.... but geez.... why no GetData(string) ??

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.)
 
Back
Top