Results 1 to 12 of 12

Thread: How do I declare a variable that'll hold bytes?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    167

    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

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    167

    Re: How do I declare a variable that'll hold bytes?

    Sorry about the confusion. i meant bits.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    167

    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.

  5. #5
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  7. #7
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    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."

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    167

    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.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do I declare a variable that'll hold bytes?

    Quote Originally Posted by quddusaliquddus View Post
    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:
    1. Shared Sub Connect(server As [String], message As [String])
    2.    Try
    3.       ' Create a TcpClient.
    4.       ' Note, for this client to work you need to have a TcpServer
    5.       ' connected to the same address as specified by the server, port
    6.       ' combination.
    7.       Dim port As Int32 = 13000
    8.       Dim client As New TcpClient(server, port)
    9.  
    10.       ' Translate the passed message into ASCII and store it as a Byte array.
    11.       Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
    12.  
    13.       ' Get a client stream for reading and writing.
    14.       '  Stream stream = client.GetStream();
    15.       Dim stream As NetworkStream = client.GetStream()
    16.  
    17.       ' Send the message to the connected TcpServer.
    18.       stream.Write(data, 0, data.Length)
    19.  
    20.       Console.WriteLine("Sent: {0}", message)
    21.  
    22.       ' Receive the TcpServer.response.
    23.       ' Buffer to store the response bytes.
    24.       data = New [Byte](256) {}
    25.  
    26.       ' String to store the response ASCII representation.
    27.       Dim responseData As [String] = [String].Empty
    28.  
    29.       ' Read the first batch of the TcpServer response bytes.
    30.       Dim bytes As Int32 = stream.Read(data, 0, data.Length)
    31.       responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
    32.       Console.WriteLine("Received: {0}", responseData)
    33.  
    34.       ' Close everything.
    35.       stream.Close()
    36.       client.Close()
    37.    Catch e As ArgumentNullException
    38.       Console.WriteLine("ArgumentNullException: {0}", e)
    39.    Catch e As SocketException
    40.       Console.WriteLine("SocketException: {0}", e)
    41.    End Try
    42.  
    43.    Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
    44.    Console.Read()
    45. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    167

    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.

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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
  •  



Click Here to Expand Forum to Full Width