-
March 11th, 2008, 08:18 AM
#1
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?
-
March 11th, 2008, 08:30 AM
#2
Re: Dynamic byte array
do you have a delimiter or do you just want to add bytes one by one to the array
-
March 11th, 2008, 08:30 AM
#3
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
-
March 11th, 2008, 09:09 AM
#4
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.
-
March 11th, 2008, 09:24 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|