How can you convert a byte array to a hexadecimal string and vice versa?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use
Convert.ToHexStringstarting 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
Substringis the best option in combination withConvert.ToByte. See this answer for more information. If you need better performance, you must avoidConvert.ToBytebefore you can dropSubString.