Nils
Nils

Reputation: 987

Byte Array Substring

I am trying to return a substring of a byte array.

(another application is pushing data into my database, where files have a prepended GUID attached to it. I want to remove this GUID, when giving the file back to the user)

if (bytes.Length > 38)
            {
                string s = System.Text.Encoding.ASCII.GetString(bytes);
                returnBytes = Convert.FromBase64String(s.Substring(38));
            }

Is it possible to do this without Text Encoding? Maybe via Array.Copy() ?

Thanks for any advice.

Upvotes: 2

Views: 11595

Answers (2)

Cyril Durand
Cyril Durand

Reputation: 16187

You can Use Buffer.BlockCopy

    Byte[] fileBytes = new Byte[bytes.Length - 16];
    Buffer.BlockCopy(bytes, 16, fileBytes, 0, fileBytes.Length); 

BTW Guid is normally 16 bytes length, 38 is string length

Upvotes: 2

Utsav Dawn
Utsav Dawn

Reputation: 8246

For the part of returning a substring of an array, you can use ArraySegment<T>. Refer to this link

Upvotes: 1

Related Questions