From Hex String to String - CSharp System

CSharp examples for System:Hex

Description

From Hex String to String

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*from ww w .  j  a va2 s .c  o  m*/

public class Main{
        // Based on https://msdn.microsoft.com/en-us/library/bb311038.aspx
        public static string FromHex(this string str)
        {
            List<string> pairs = new List<string>();
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < str.Length - 3; i += 2)
                pairs.Add(str.Substring(i, 2));

            foreach (string strVal in pairs.Select(s => Convert.ToInt32(s, 16)).Select(char.ConvertFromUtf32))
            {
                sb.Append(strVal);
            }

            return sb.ToString();
        }
}

Related Tutorials