Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# equivelant to Java Arrays.ToString(byte[])

Tags:

java

arrays

c#

Is there an equivalent method in C# to the Java method Arrays.ToString(byte[])

Found here

Essentially I want to convert a byte array to a string of the format:

"[10, 23, 0, 15]"

The numbers reflecting the value of each byte in the byte array.

like image 775
William Pownall Avatar asked Dec 04 '25 05:12

William Pownall


1 Answers

It's a one-liner. Try this:

static string Array2String<T>( IEnumerable<T> list )
{
  return "[" + string.Join(",",list) + "]";
}

You might need to tweak it a bit for different flavors of T to allow for proper quoting and/or stringification1 etc., but that's the general principle.

1 Not all types have a ToString() that comes back with anything terribly useful since object just hands back the type name.

like image 83
Nicholas Carey Avatar answered Dec 06 '25 23:12

Nicholas Carey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!