﻿ C# - Convert ASCII text to hexadecimal value as a string

# C# Sharp Exercises: Convert ASCII text to hexadecimal value as a string

## C# Sharp String: Exercise-67 with Solution

Write a C# Sharp program that converts a given string’s character as ASCII to hexadecimal value as a string.

Sample Data:
("Python") -> "50 79 74 68 6f 6e"
("Century of the year") -> 43 65 6e 74 75 72 79 20 6f 66 20 74 68 65 20 79 65 61 72
("CPP Exercises") -> 43 50 50 20 45 78 65 72 63 69 73 65 73

Sample Solution:

C# Sharp Code:

``````using System;
using System.Linq;

namespace exercises
{
class Program
{
static void Main(string[] args)
{
string text = "Python";
Console.WriteLine("Original strings: " + text);
Console.WriteLine("Convert said ASCII text to hexadecimal value as a string:" + test(text));
text = "Century of the year";
Console.WriteLine("\nOriginal strings: " + text);
Console.WriteLine("Convert said ASCII text to hexadecimal value as a string:" + test(text));
text = "CPP Exercises";
Console.WriteLine("\nOriginal strings: " + text);
Console.WriteLine("Convert said ASCII text to hexadecimal value as a string:" + test(text));
}

public static string test(string text)
{
return text.ToCharArray().Aggregate("", (a, b) => a + ((byte)b).ToString("X") + " ").ToLower().Trim();
}
}
}
```
```

Sample Output:

```Original strings: Python
Convert said ASCII text to hexadecimal value as a string:50 79 74 68 6f 6e

Original strings: Century of the year
Convert said ASCII text to hexadecimal value as a string:43 65 6e 74 75 72 79 20 6f 66 20 74 68 65 20 79 65 61 72

Original strings: CPP Exercises
Convert said ASCII text to hexadecimal value as a string:43 50 50 20 45 78 65 72 63 69 73 65 73
```

Flowchart :

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous C# Sharp Exercise: Highest frequency character(s) in the words of a string.
Next C# Sharp Exercise: Check if a string has all unique characters.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿