.Net5.0 Update
Thanks to @antoninkriz's benchmark comparison we can see that Convert.ToHexString is by far the clear winner today
You'd be foolish to use anything other than Convert.ToHexString. In my opinion it clearly wins in: readability, performance, safety
The rest of this is from before Apr 10, 2012:
- Took each bit of code from an answer here:
- BitConvertRep = Answer by Guffa, BitConverter and String.Replace (I'd recommend for most cases, [edit:] where you can't use
Convert.ToHexString) - StringBuilder = Answer by Quintin Robinson, foreach char StringBuilder.Append
- LinqConcat = Answer by Michael Buen, string.Concat of Linq built array
- LinqJoin = Answer by mloskot, string.Join of Linq built array
- LinqAgg = Answer by Matthew Whited, IEnumerable.Aggregate with StringBuilder
- ToHex = Answer by Kurt, sets chars in an array, using byte values to get hex
- ByteArrayToHexString = Answer by Nathan Moinvaziri, approx same speed as the ToHex above, and is probably easier to read (I'd recommend for speed, [edit:] where you can't use
Convert.ToHexString) - ToHexFromTable = Linked in answer by Nathan Moinvaziri, for me this is near the same speed as the above 2 but requires an array of 256 strings to always exist