Hex String To UInt - CSharp System

CSharp examples for System:Hex

Description

Hex String To UInt

Demo Code

// Licensed under the GPL license.
using System.Globalization;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from   ww w  . j  ava2s  .  c  om

public class Main{
        public static ushort HexStringToUInt16(string pvVal)
        {
            ushort num = 0;
            if (pvVal == null)
            {
                return num;
            }
            int discarded = 0;
            if (pvVal.Length == 2)
            {
                pvVal = "00" + pvVal;
            }
            if (pvVal.Length != 4)
            {
                return 0;
            }
            num = BitConverter.ToUInt16(GetBytes(pvVal, out discarded), 0);
            return (ushort)((num >> 8) | (num << 8));
        }
}

Related Tutorials