byte[] to ArrayList
-
Dear all, I'm stuck once again and calling on you for aid. I want to convert a byte array to an arraylist. I managed to write some code that will put my byte[] data in a file :
Byte\[\] result = (Byte\[\])foundRows\[0\]\["bin\_file"\]; using (BinaryWriter binWriter = new BinaryWriter(File.Open("@C:\\test.txt", FileMode.Create))) { binWriter.Write(result); }
This works fine but I would like to write it to an ArrayList so I can process it from there. Reading it back from the file into an ArrayList seems unlogical for me and unneccesary too.
ArrayList list = new ArrayList(); StreamReader se = new StreamReader("@C:\\test.txt"); string line = se.ReadLine(); while (line != null) { list.Add(line); //Read the next line line = se.ReadLine(); } se.Close();
Can somebody point me in the right direction on how to tackle this problem ? With other words, how can I put my byte[] data directly into an ArrayList. Kind regards, Rick
-
Dear all, I'm stuck once again and calling on you for aid. I want to convert a byte array to an arraylist. I managed to write some code that will put my byte[] data in a file :
Byte\[\] result = (Byte\[\])foundRows\[0\]\["bin\_file"\]; using (BinaryWriter binWriter = new BinaryWriter(File.Open("@C:\\test.txt", FileMode.Create))) { binWriter.Write(result); }
This works fine but I would like to write it to an ArrayList so I can process it from there. Reading it back from the file into an ArrayList seems unlogical for me and unneccesary too.
ArrayList list = new ArrayList(); StreamReader se = new StreamReader("@C:\\test.txt"); string line = se.ReadLine(); while (line != null) { list.Add(line); //Read the next line line = se.ReadLine(); } se.Close();
Can somebody point me in the right direction on how to tackle this problem ? With other words, how can I put my byte[] data directly into an ArrayList. Kind regards, Rick
Arraylists are a bit old fasioned these days. You should be using some form of generic list instead. 2 easy ways.
byte\[\] byteArray = new byte\[5\]; List<byte> byteList1 = byteArray.ToList(); List<byte> byteList2 = new List<byte>(byteArray);
- In .net 3.5, there is a ToList() extension method on arrays. 2) One of the constructor for List<> takes an array.
Simon
-
Dear all, I'm stuck once again and calling on you for aid. I want to convert a byte array to an arraylist. I managed to write some code that will put my byte[] data in a file :
Byte\[\] result = (Byte\[\])foundRows\[0\]\["bin\_file"\]; using (BinaryWriter binWriter = new BinaryWriter(File.Open("@C:\\test.txt", FileMode.Create))) { binWriter.Write(result); }
This works fine but I would like to write it to an ArrayList so I can process it from there. Reading it back from the file into an ArrayList seems unlogical for me and unneccesary too.
ArrayList list = new ArrayList(); StreamReader se = new StreamReader("@C:\\test.txt"); string line = se.ReadLine(); while (line != null) { list.Add(line); //Read the next line line = se.ReadLine(); } se.Close();
Can somebody point me in the right direction on how to tackle this problem ? With other words, how can I put my byte[] data directly into an ArrayList. Kind regards, Rick
You have an array and need to represent it in a ArrayList. Right ? You can use
List<T>
.byte[] yourArray = ...;
List<byte> list = new List<byte>(yourArray);This will insert all the array elements to the list. To get items, you can iterate items on the list.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions