Results 1 to 3 of 3

Thread: [2.0] Remove null bytes from an array?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    [2.0] Remove null bytes from an array?

    I have

    Code:
    1. byte[] buf = new bytes[256];
    2.                 bytesrec = TCP.Client.Receive(buf,System.Net.Sockets.SocketFlags.None);

    But the problem is when there is a packet sent with less then 256 bytes it has empty bytes on the end. How do i format it to remove those bytes?

  2. #2
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: [2.0] Remove null bytes from an array?

    Hi, I had an old project that used Byte arrays alot, I seem to remember something like you can prefix bytes with a *, ie.
    Code:
    *bytes
    And it will create the array members as you need them.

    I don't know if it was that exactly, and I don't know if you're using 256 for a reason (limit size)?

    Either way, hope that helps, I'll try to find that project to show you better, more usefull code.
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

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

    Re: [2.0] Remove null bytes from an array?

    You don't remove them. A byte array with 256 elements has 256 bytes in it. The idea is that the 'bytesrec' variable contains the number of btes that were actually received. You use that number to know how many elements of the array to actually use. If 'bytesrec' is 256 you know the array is full, otherwise you only use the first 'bytesrec' elements. If you need a full array for some reason then you have to create a new byte array with 'bytesrec' elements and then copy the first 'bytesrec' elements from the original array.
    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

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