Hi :D,
I would like to know how to declare a variable that holds bits.
Thanks :)
Printable View
Hi :D,
I would like to know how to declare a variable that holds bits.
Thanks :)
bits or bytes? you say bytes in the title and bits in the subject.
How about the Byte data type?
Sorry about the confusion. i meant bits.
I know theres something called an array of bytes - but i dont want an array.
Well bits are just binary - 0s or 1s so in that respect you could use booleans to represent them.
I'm not really sure what your question is.
How about Integer? After all, an Integer is just 32 bits (though if you fiddle with the most significant bit then odd things happen, so work with an unsigned integer or just deal with the first 31 bits). Toggling any or all bits is easy enough to do via masks and the like.
Or even a BitArray or a BitVector32:
Dim b As New BitArray(1066)
Dim bv As New System.Collections.Specialized.BitVector32
Thanks for the suggestions. I should really have clarified: I want to send packets of data over a tcp connection and was informed that bytes are the way to go. But to save on size of the data packets - i would prefer to use bits. Does that make sense? Im not sure if im on the right track.
You would only gain by using bits if the data in the packet was VERY strange. However, you could have the data in a class, serialize the class using a binary serializer, then compress it. There are some serious drawbacks to that design, though, since binary serialization adds on information about the type and the assembly that did the serializing, which means that the class types would have to be in a dll shared between the routine that serializes and the routine that deserializes.
Compressing a stream is possible, as well, so the serialization might be left out, but it depends on the data.
For most data types, bytes are the lowest level that you can reasonably go to. For example, an Integer takes up four bytes, but if the number is between 0 and 65,000 (plus some change), then only two of the four bytes holds data, while the other two bytes are both 0. Technically, then, you could stuff more information into those unused bytes, but you'd also need to include information as to how to interpret the bytes on the other end, which may take more space than you gained. Even the compression routines available in .NET, which will compress a stream, will sometimes result in the data being expanded in size.
Um, yes you do. Here's the code example provided in the documentation for the TcpClient class:Note the lines that convert the data into binary:vb.net Code:
Shared Sub Connect(server As [String], message As [String]) Try ' Create a TcpClient. ' Note, for this client to work you need to have a TcpServer ' connected to the same address as specified by the server, port ' combination. Dim port As Int32 = 13000 Dim client As New TcpClient(server, port) ' Translate the passed message into ASCII and store it as a Byte array. Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message) ' Get a client stream for reading and writing. ' Stream stream = client.GetStream(); Dim stream As NetworkStream = client.GetStream() ' Send the message to the connected TcpServer. stream.Write(data, 0, data.Length) Console.WriteLine("Sent: {0}", message) ' Receive the TcpServer.response. ' Buffer to store the response bytes. data = New [Byte](256) {} ' String to store the response ASCII representation. Dim responseData As [String] = [String].Empty ' Read the first batch of the TcpServer response bytes. Dim bytes As Int32 = stream.Read(data, 0, data.Length) responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes) Console.WriteLine("Received: {0}", responseData) ' Close everything. stream.Close() client.Close() Catch e As ArgumentNullException Console.WriteLine("ArgumentNullException: {0}", e) Catch e As SocketException Console.WriteLine("SocketException: {0}", e) End Try Console.WriteLine(ControlChars.Cr + " Press Enter to continue...") Console.Read() End Sub 'Connectand then send that data over the TCP connection:Code:' Translate the passed message into ASCII and store it as a Byte array.
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
That's how you work with binary data in .NET apps: Byte arrays.Code:' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)
Thank you all for your help.
jmcilhinney - I guess you must be right as storing each byte seperately/and or sending it seperately along the same stream will result in a larger packet im guessing.
If you sent each byte separately, then you would send each byte in its own packet, so you'd get the packet overhead on each byte, which wouldn't be as efficient. As it is, if you send out a large whack of data in a byte array, TCP will chunk it into packets, as needed by size, add the necessary headers, and re-sew the packets together again on the other end. Sending each byte as its own message would mean that TCP wouldn't fragment or re-sew anything, but you'd get the packet header on every single byte, which would magnify the total amount sent by a huge amount, while making it necessary for you to do the sewing.