|
-
May 12th, 2009, 04:30 PM
#1
Thread Starter
Addicted Member
How do I declare a variable that'll hold bytes?
Hi ,
I would like to know how to declare a variable that holds bits.
Thanks
-
May 12th, 2009, 04:49 PM
#2
Re: How do I declare a variable that'll hold bytes?
bits or bytes? you say bytes in the title and bits in the subject.
How about the Byte data type?
-
May 12th, 2009, 04:50 PM
#3
Thread Starter
Addicted Member
Re: How do I declare a variable that'll hold bytes?
Sorry about the confusion. i meant bits.
-
May 12th, 2009, 04:51 PM
#4
Thread Starter
Addicted Member
Re: How do I declare a variable that'll hold bytes?
I know theres something called an array of bytes - but i dont want an array.
-
May 12th, 2009, 04:54 PM
#5
Re: How do I declare a variable that'll hold bytes?
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.
-
May 12th, 2009, 04:55 PM
#6
Re: How do I declare a variable that'll hold bytes?
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.
My usual boring signature: Nothing
 
-
May 12th, 2009, 05:11 PM
#7
Re: How do I declare a variable that'll hold bytes?
Or even a BitArray or a BitVector32:
Dim b As New BitArray(1066)
Dim bv As New System.Collections.Specialized.BitVector32
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
May 12th, 2009, 05:35 PM
#8
Thread Starter
Addicted Member
Re: How do I declare a variable that'll hold bytes?
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.
-
May 12th, 2009, 06:08 PM
#9
Re: How do I declare a variable that'll hold bytes?
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.
My usual boring signature: Nothing
 
-
May 12th, 2009, 10:34 PM
#10
Re: How do I declare a variable that'll hold bytes?
 Originally Posted by quddusaliquddus
I know theres something called an array of bytes - but i dont want an array.
Um, yes you do. Here's the code example provided in the documentation for the TcpClient class:
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 'Connect
Note the lines that convert the data into binary:
Code:
' Translate the passed message into ASCII and store it as a Byte array.
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
and then send that data over the TCP connection:
Code:
' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)
That's how you work with binary data in .NET apps: Byte arrays.
-
May 13th, 2009, 10:43 AM
#11
Thread Starter
Addicted Member
Re: How do I declare a variable that'll hold bytes?
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.
-
May 13th, 2009, 11:09 AM
#12
Re: How do I declare a variable that'll hold bytes?
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.
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|