All pastes #742606 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-10-19 17:39 UTC
rendered paste body
        public string getHex(string tString)
        {
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            Byte[] bytes = encoding.GetBytes(tString);
            return ToHexString(bytes);
        }

        public static string ToHexString(byte[] bytes)
        {
            char[] hexDigits = {
            '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
            };
            
            char[] chars = new char[bytes.Length * 2];

            for (int i = 0; i < bytes.Length; i++)
            {
                int b = bytes[i];
                chars[i * 2] = hexDigits[b >> 4];
                chars[i * 2 + 1] = hexDigits[b & 0xF];
            }
            return new string(chars);
        }