none
byte RRS feed

  • Question

  • I have a server-client connection that where in, serverside send 204,000 bytes in two hundred byte[] array to clinetsie,and  I'm going to merge these two hundred in the clinetside 
    at one byte[] array,Can i dp that?help me me please






    • Edited by Ali_Lonbar Monday, February 24, 2020 1:08 PM
    Monday, February 24, 2020 11:59 AM

All replies

  • So, if I have understood the question correctly, you have two arrays of byte and you want to combine both into a one array.

    Since the size of arrays cannot be changed in .NET, you have to create a new array whose size is the sum of the sizes of the two arrays that you want to combine.

    Then, you can use two calls to the method Array.Copy to copy the original arrays into the new one.

    https://docs.microsoft.com/en-us/dotnet/api/system.array.copy?view=netframework-4.8

    As you can see, there are several overloads of the method, which allow you to copy the original array into a specific position in the target array. So you will copy the first of your arrays into position 0 of the target, and then the second array into the position that is equal to the Length of the first array. This will yield a target array that contains the merged contents of the two original arrays.

    Monday, February 24, 2020 2:18 PM
    Moderator

  • Since the size of arrays cannot be changed in .NET, you have to create a new array whose size is the sum of the sizes of the two arrays that you want to combine.



    Array.Resize<T>(T[], Int32) Method
    https://docs.microsoft.com/en-us/dotnet/api/system.array.resize?view=netframework-4.8

    - Wayne

    Monday, February 24, 2020 3:41 PM
  • Array.Resize<T>(T[], Int32) Method

    From the "Remarks" section in the document:

    "This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one."

    It does not really change the size, it just allocates a new array and copies the elements to it, then discards the old one.

    Monday, February 24, 2020 4:17 PM
    Moderator
  • :

    "This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one."

    It does not really change the size, it just allocates a new array and copies the elements to it, then discards the old one.

    Exactly. Which is common behaviour when resizing an array, vector, etc. as the
    functions/methods seek to make the allocation units consecutive. For example
    in the C library realloc will attempt to allocate heap at the end of the
    current allocation first, but if it can't be allocated to keep the allocation
    contiguous then it will allocate a new area, relocate the data from the original,
    and return a pointer to the new heap allocation.

    But the use of Array.Resize, etc. makes it unnecessary to do the new allocations,
    data relocating, and releasing the old allocations, etc. manually/explicitly.
    It is handled for you.

    - Wayne

    Monday, February 24, 2020 6:38 PM