Home
Manage Your Code
Snippet: Convert a Hex string to Ascii (C#)
Title: Convert a Hex string to Ascii Language: C#
Description: Converts a Hex encoded string to Ascii Views: 336
Author: Shaun Elston Date Added: 8/28/2008
Copy Code  
1/// <summary>
2        /// Converts a Hex string to Ascii.
3        /// Private helper method.
4        /// </summary>
5        /// <param name="Data">A Hex encoded string</param>
6        /// <returns>An Ascii encoded string</returns>
7        private static string Hex_Asc(string Data)
8        {
9            // Create a string variable to hold each character as it's converted
10            string Data1 = string.Empty;
11
12            // Create a string variable to hold the aggregated character data.
13            string sData = string.Empty;
14
15            // While there is data left to convert
16            while (Data.Length > 0)
17            {
18                // Take a hex pair from the Data string passed in, convert to UInt from base 16, convert
19                // the int to a Char and output it as a string
20                Data1 = System.Convert.ToChar(System.Convert.ToUInt32(Data.Substring(0, 2), 16)).ToString();
21
22                // Add the character onto our already converted data
23                sData = sData + Data1;
24
25                // Remove the hex pair we just converted from the data passed in
26                Data = Data.Substring(2, Data.Length - 2);
27
28            }
29
30            // Return the converted string
31            return sData;
32        }
Usage
string asciiString = Hex_Asc(hexData);