CSharp examples for System:Byte
Converts a hexadecimal string to a byte array.
using System.Text; using System.Runtime.CompilerServices; using System.Reflection; using System.Linq; using System.Globalization; using System.Collections.Generic; using System;//from w w w. j a v a 2s. c o m public class Main{ /// <summary> /// Converts a hexadecimal string to a byte array. /// </summary> /// <param name="hex">The hexadecimal string to convert.</param> /// <returns>The byte array representing the hexadecimal string.</returns> public static byte[] HexToByteArray(this string hex) { if (hex[0] == '0' && (hex[1] == 'x' || hex[1] == 'X')) { hex = hex.Substring(2); } byte[] result = new byte[hex.Length / 2]; for (int i = 0; i < result.Length; i++) { result[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16); } return result; } }