How can you convert a byte array to a hexadecimal string, and vice versa?
C# – How to convert a byte array to a hexadecimal string, and vice versa
arrayschex
Related Topic
- C# – How to convert UTF-8 byte[] to string
- Javascript – Checking if a key exists in a JavaScript object
- Javascript – Sort array of objects by string property value
- Java – How to declare and initialize an array in Java
- Javascript – Loop through an array in JavaScript
- Javascript – How to check if an object is an array?
- Javascript – For-each over an array in JavaScript
Best Answer
You can use
Convert.ToHexString
starting with .NET 5.There's also a method for the reverse operation:
Convert.FromHexString
.For older versions of .NET you can either use:
or:
There are even more variants of doing it, for example here.
The reverse conversion would go like this:
Using
Substring
is the best option in combination withConvert.ToByte
. See this answer for more information. If you need better performance, you must avoidConvert.ToByte
before you can dropSubString
.