c# how to add byte to byte array

前端 未结 6 614
梦毁少年i
梦毁少年i 2020-12-08 15:30

How to add a byte to the beginning of an existing byte array? My goal is to make array what\'s 3 bytes long to 4 bytes. So that\'s why I need to add 00 padding in the beginn

相关标签:
6条回答
  • 2020-12-08 16:04

    Simple, just use the code below, as I do:

            public void AppendSpecifiedBytes(ref byte[] dst, byte[] src)
        {
            // Get the starting length of dst
            int i = dst.Length;
            // Resize dst so it can hold the bytes in src
            Array.Resize(ref dst, dst.Length + src.Length);
            // For each element in src
            for (int j = 0; j < src.Length; j++)
            {
                // Add the element to dst
                dst[i] = src[j];
                // Increment dst index
                i++;
            }
        }
    
        // Appends src byte to the dst array
        public void AppendSpecifiedByte(ref byte[] dst, byte src)
        {
            // Resize dst so that it can hold src
            Array.Resize(ref dst, dst.Length + 1);
            // Add src to dst
            dst[dst.Length - 1] = src;
        }
    
    0 讨论(0)
  • 2020-12-08 16:10

    To prevent recopy the array every time which isn't efficient

    What about using Stack

    csharp> var i = new Stack<byte>();
    csharp> i.Push(1);
    csharp> i.Push(2); 
    csharp> i.Push(3); 
    csharp> i; { 3, 2, 1 }
    
    csharp> foreach(var x in i) {
      >       Console.WriteLine(x);
      >     }
    

    3 2 1

    0 讨论(0)
  • 2020-12-08 16:12

    As many people here have pointed out, arrays in C#, as well as in most other common languages, are statically sized. If you're looking for something more like PHP's arrays, which I'm just going to guess you are, since it's a popular language with dynamically sized (and typed!) arrays, you should use an ArrayList:

    var mahByteArray = new ArrayList<byte>();
    

    If you have a byte array from elsewhere, you can use the AddRange function.

    mahByteArray.AddRange(mahOldByteArray);
    

    Then you can use Add() and Insert() to add elements.

    mahByteArray.Add(0x00); // Adds 0x00 to the end.
    mahByteArray.Insert(0, 0xCA) // Adds 0xCA to the beginning.
    

    Need it back in an array? .ToArray() has you covered!

    mahOldByteArray = mahByteArray.ToArray();
    
    0 讨论(0)
  • 2020-12-08 16:19

    Although internally it creates a new array and copies values into it, you can use Array.Resize<byte>() for more readable code. Also you might want to consider checking the MemoryStream class depending on what you're trying to achieve.

    0 讨论(0)
  • 2020-12-08 16:20

    You can't do that. It's not possible to resize an array. You have to create a new array and copy the data to it:

    bArray = addByteToArray(bArray,  newByte);
    

    code:

    public byte[] addByteToArray(byte[] bArray, byte newByte)
    {
        byte[] newArray = new byte[bArray.Length + 1];
        bArray.CopyTo(newArray, 1);
        newArray[0] = newByte;
        return newArray;
    }
    
    0 讨论(0)
  • 2020-12-08 16:21

    Arrays can't be resized, so you need to allocte a new array that is larger, write the new byte at the beginning of it, and use Buffer.BlockCopy to transfer the contents of the old array across.

    0 讨论(0)
提交回复
热议问题