C#Sixteen -in -made string to the conversion of byte array
In C#, converting the hexadecimal string into byte array can be implemented to achieve the combination of linq (language integration query) and string operation.
The following is an example code:
This method accepts a hexadecimal string as input and executes the following steps:
<code class="language-csharp">public static byte[] HexStringToByteArray(string hex) { return Enumerable.Range(0, hex.Length) .Where(x => x % 2 == 0) .Select(x => Convert.ToByte(hex.Substring(x, 2), 16)) .ToArray(); }</code>
It uses to generate the index sequence of characters in the string.
Enumerable.Range(0, hex.Length)
Where(x => x % 2 == 0)
hex.Substring(x, 2)
Convert.ToByte(hex.Substring(x, 2), 16)
This method uses the powerful function of Linq, and demonstrates a method for effective analysis in C#and converting the hexadecimal string into byte array. The above is the detailed content of How to Efficiently Convert a Hex String to a Byte Array in C#?. For more information, please follow other related articles on the PHP Chinese website!