none
Remove byte from an byte array RRS feed

  • Question

  •  Hi i have a bytearray as following parameter

    DeSerialize(byte[] stream)

    i have to remove last byte from the byte array. can any one help me

    Tuesday, August 7, 2012 8:09 AM

Answers

All replies

  • Why don't you use .RemoveAt(index) ?
    Tuesday, August 7, 2012 8:17 AM
  • Hi,

    Since the array has fixed length, you cannot remove element from it. You can assign null value to an element, but the element will be still there. If you really need to remove it, you can create a shorter array, copy all the element to the new array what you would like to keep, and work with the new array.

    Tuesday, August 7, 2012 8:18 AM
  • Can u please help me with an example
    Tuesday, August 7, 2012 8:21 AM
  • Can u please help me with an example


    public static byte[]DeSerialize(byte[] stream) { stream.RemoveAt(stream.Length-1); return stream; }

    U can call this:

    byte[] result = DeSerialize(Ur bytes collection);


    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    Tuesday, August 7, 2012 8:23 AM
  • Hi,

    this works best if you convert your Array to a List, so you can use the RemoveAt(index) method, the convert it back to an array. Take this example and use it for your own purpose:

            static void Main(string[] args)
            {
                byte[] stream = { 1, 0, 1, 0 };
    
                for (int i = 0; i < stream.Length; i++)
                {
                    Console.WriteLine(stream[i].ToString());
                }
    
                int length = stream.Length;
    
                var liste = new List<byte>(stream);
                liste.RemoveAt(length - 1);
    
                byte[] newstream = liste.ToArray();
    
                Console.WriteLine();
                for (int i = 0; i < newstream.Length; i++)
                {
                    Console.WriteLine(newstream[i].ToString());
                }
            }

    Regards,

    Steve.


    • Edited by Steve Koenig Tuesday, August 7, 2012 8:25 AM typo in code
    Tuesday, August 7, 2012 8:24 AM
    • Marked as answer by Jason Dot Wang Wednesday, August 15, 2012 8:15 AM
    Tuesday, August 7, 2012 8:24 AM
  • public static byte[]DeSerialize(byte[] stream) { stream.RemoveAt(stream.Length-1); return stream; }

    There is no RemoveAt() method for plain arrays...
    Tuesday, August 7, 2012 8:33 AM
  • I think it could work
    Array.Copy(stream, stream, stream.Length - 1);
    Array.Resize(ref stream, stream.Length - 1);

    Tuesday, August 7, 2012 8:37 AM
  • Resize the array: 
          Array.Resize<byte>(ref stream, stream.Length-1);

    • Proposed as answer by Balaji Baskar Wednesday, August 8, 2012 11:31 AM
    • Marked as answer by Jason Dot Wang Wednesday, August 15, 2012 8:15 AM
    Tuesday, August 7, 2012 10:19 AM
  • i understood how to remove last byte from array, nowhelp me in How to remove 1st byte from the array?
    Tuesday, August 7, 2012 10:53 AM
  •             byte[] stream = { 1, 0, 1, 0 };
    
                for (int i = 0; i < stream.Length; i++)
                {
                    Console.WriteLine(stream[i].ToString());
                }
    
                var liste = new List<byte>(stream);
    
                liste.RemoveAt(0);
    
                byte[] newstream = liste.ToArray();
    
                Console.WriteLine();
                for (int i = 0; i < newstream.Length; i++)
                {
                    Console.WriteLine(newstream[i].ToString());
                }
    
    

    Almost the same like I showed you before.
    • Marked as answer by Avatar 123 Wednesday, August 8, 2012 11:39 AM
    Tuesday, August 7, 2012 11:02 AM
  • byte[] originalArray = ....
    
    byte[] missingFirstByte = originalArray.Skip(1).ToArray();
    

    Tuesday, August 7, 2012 2:08 PM
  • <Super-Nitpicking mode>

    Instances of System.Array support an explicit implementation of IList.RemoveAt
    and of IList<T>.RemoveAt -  which of course throws an NotSupportedException for
    fixed sized arrays.

    </Super-Nitpicking mode>



    Tuesday, August 7, 2012 2:20 PM