SoFunction
Updated on 2025-03-06

C# operates the Byte array and hexadecimal to convert each other

1. Byte array to hexadecimal string

 		/// <summary>
        /// Byte array to hexadecimal string        /// </summary>
        /// <param name="Bytes"></param>
        /// <returns></returns>
        public static string ByteToHex(byte[] Bytes)
        {
            string str = ;
            foreach (byte Byte in Bytes)
            {
                str += ("{0:X2}", Byte) + " ";
            }
            return ();
        }

2. To string to hexadecimal Byte array

        /// <summary>
        /// To string to hexadecimal Byte array        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] strToToHexByte(string hexString)
        {
            try
            {
                hexString = (" ", "");
                if (( % 2) != 0)
                    hexString += " ";
                byte[] returnBytes = new byte[ / 2];
                for (int i = 0; i < ; i++)
                    returnBytes[i] = ((i * 2, 2), 16);
                return returnBytes;
            }
            catch
            {
                return null;
            }

        }

This is all about this article about C# manipulating the Byte array and hexadecimal to convert it into each other. I hope it will be helpful to everyone's learning and I hope everyone will support me more.