Edward
Edward

Reputation: 7424

How to reverse the order of a byte array in c#?

How do you reverse the order of a byte array in c#?

Upvotes: 19

Views: 71207

Answers (5)

Bala R
Bala R

Reputation: 109027

You can use the Array.Reverse() method.

Upvotes: 2

Shankar Raju
Shankar Raju

Reputation: 4546

You can use Array.Reverse. Also please go through this for more information.

Upvotes: 2

Harry Steinhilber
Harry Steinhilber

Reputation: 5239

You could use the Array.Reverse method:

byte[] bytes = GetTheBytes();
Array.Reverse(bytes, 0, bytes.Length);

Or, you could always use LINQ and do:

byte[] bytes = GetTheBytes();
byte[] reversed = bytes.Reverse().ToArray();

Upvotes: 54

Muad'Dib
Muad'Dib

Reputation: 29286

you can use the linq method: MyBytes.Reverse() as well as the Array.Reverse() method. Which one you should use depends on your needs.

The main thing to be aware of is that the linq version will NOT change your original. the Array version will change your original array.

Upvotes: 8

Mark Cidade
Mark Cidade

Reputation: 100047

Array.Reverse(byteArray);

 

Upvotes: 10

Related Questions