Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Jarrod Sharp
Guest
 
Posts: n/a
#1: Nov 22 '05
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_DataArrival Event) 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



Jarrod Sharp
Guest
 
Posts: n/a
#2: Nov 22 '05

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


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)
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#3: Nov 22 '05

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


Jarrod Sharp <JarrodSharp@discussions.microsoft.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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Closed Thread