Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. byte[] to ArrayList

byte[] to ArrayList

Scheduled Pinned Locked Moved C#
helpquestiondata-structurestutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rick van Woudenberg
    wrote on last edited by
    #1

    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

    S N 2 Replies Last reply
    0
    • R Rick van Woudenberg

      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

      S Offline
      S Offline
      Simon P Stevens
      wrote on last edited by
      #2

      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);
      
      1. In .net 3.5, there is a ToList() extension method on arrays. 2) One of the constructor for List<> takes an array.

      Simon

      1 Reply Last reply
      0
      • R Rick van Woudenberg

        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

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups