Convert byte array to hex string

Byte array

A byte encompasses 8 bits, and a byte array, as the name suggests, constitutes an array of bytes. This byte array proves indispensable for storing binary data collections, encompassing diverse sources like data files, image files, compressed files, downloaded server responses, and an array of other file types. However, it is essential to consider employing streaming data types when handling substantial volumes of binary data, particularly if the programming language supports such a feature. Nonetheless, byte arrays present an optimal means of representation for this data, as they efficiently capture low-level representations, offering valuable flexibility in method optimization. By utilizing byte arrays, developers can utilize the power of low-level data handling, enhancing the performance of certain operations and ensuring the efficient storage and manipulation of binary information.


byte array to string hexa

Hexadecimal string

Characters are represented as ASCII and they are 1 byte long, a byte could be represented with 2 digits in hexadecimal format for example 255 is 0xFF.

Convert a byte array to a hexadecimal string

byte[] bArray = { 1, 2, 4, 8, 16, 32 }; string strHex = BitConverter.ToString(bArray);
output
01-02-04-08-10-20

If you want it without the dashes, just remove them:

byte[] bArray = { 1, 2, 4, 8, 16, 32 }; string strHex = BitConverter.ToString(bArray).Replace("-", string.Empty);
output
010204081020

Convert a String Hex to byte array

using System; using System.Collections; using System.Linq; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { byte[] bArray = { 192, 168, 1, 1 }; string strHex = BitConverter.ToString(bArray).Replace("-", string.Empty); Console.WriteLine(strHex); Console.ReadKey(); byte[] bytes = new byte[strHex.Length / 2]; for (int i = 0; i < strHex.Length; i += 2) Console.WriteLine(bytes[i / 2] = Convert.ToByte(strHex.Substring(i, 2), 16)); Console.ReadKey(); } } }
output
C0A80101 192 168 1 1