CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Jul 2002
    Posts
    788

    Dynamic byte array

    I need to use a dynamic byte array. How to declare a dynamic byte array in csharp?

    for example:
    Code:
    //This is fixed size.
    byte[] l_byteArray = new byte[100];
    How can i declare a dynamic byte array where i can add any number of bytes to an existing byte array?

  2. #2
    Join Date
    Sep 2007
    Posts
    82

    Re: Dynamic byte array

    do you have a delimiter or do you just want to add bytes one by one to the array

  3. #3
    Join Date
    Mar 2008
    Location
    Atlanta, GA
    Posts
    49

    Re: Dynamic byte array

    You could always use:

    Code:
    List<byte> list = new List<byte>();
    This gives you type safety and the ability to insert, remove, etc.

    Although maybe you're looking for something a little slicker, but unfortunately I don't think an array is what you want to use since you're constrained by the size you define it by at run-time.

    Edit: Oh, and you can always convert it back to a byte[] by calling list.ToArray()
    Last edited by opedog; March 11th, 2008 at 08:32 AM. Reason: quick addition

  4. #4
    Join Date
    Jul 2002
    Posts
    788

    Re: Dynamic byte array

    Thanks!

    another question;

    Instead of copying byte by byte myself, is there a function (more efficient) to append a byte array to a stack of byte, where the last byte in the byte array i want to copy, such as byteArr[99] is appended to the bottom of the stack.

    Code:
    byte [] byteArr = new byte[100];
    byte [] byteArr2 = new byte[100];
    Stack<byte> stackByteStream = new Stack<byte>();
    I want to copy byteArr and byteArr2 to stackByteStream to process the buffer.

  5. #5
    Join Date
    Mar 2008
    Location
    Atlanta, GA
    Posts
    49

    Re: Dynamic byte array

    When you speak of Stacks, using a term such as append really shouldn't apply. Stacks are a very specific data structure and should really only be modified via the Push, Pop, or Clear calls.

    I'd say stick with the List<byte> and just do AddRange calls so you can avoid iterating every time you need to insert (or append) a List<byte> into it.

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

Featured