Converts 1 or 2 character string into equivalent byte value - CSharp System

CSharp examples for System:Converter

Description

Converts 1 or 2 character string into equivalent byte value

Demo Code


using System;/*from  ww w.j  a  v  a  2 s  . c om*/

public class Main{
        /// <summary>
        /// Converts 1 or 2 character string into equivalent byte value
        /// </summary>
        /// <param name="hex">1 or 2 character string</param>
        /// <returns>byte</returns>
        private static byte HexToByte(string hex)
        {
            if (hex.Length > 2 || hex.Length <= 0)
                throw new ArgumentException("hex must be 1 or 2 characters in length");

            byte newByte = byte.Parse(hex, System.Globalization.NumberStyles.HexNumber);
            return newByte;
        }
}

Related Tutorials