So I want to make "hello world!" in a creative way and I came up with and idea to use ASCII but I don’t realy now how to convert from char to ASCII symbol (neither from string). Here is my code:
public static void Main()
{
List<string> imie = new List<string>();
greet();
}
public static string greet()
{
string s = "";
string nums = "104 101 108 108 111 32 119 111 114 108 100 33";
char[] numbers = nums.ToCharArray();
foreach (char l in numbers)
{
s += Encoding.ASCII.GetChars(new byte[] {l});
}
return s;
}
Also in line "s += Encoding.ASCII.GetChars(new byte[] {l});" I am getting error "Cannot implicitly convert type ‘char’ to ‘byte’. An explicit conversion exists (are you missing a cast?)"
>Solution :
here you go
public static string greet() {
string s = "";
string nums = "104 101 108 108 111 32 119 111 114 108 100 33";
var numbers = nums.Split(" ");
foreach (var nstr in numbers) {
int k = Int32.Parse(nstr);
s += Convert.ToChar(k);
}
return s;
}
or better (appending to a string is very ineffiecint)
public static string greet() {
StringBuilder s = "";
string nums = "104 101 108 108 111 32 119 111 114 108 100 33";
var numbers = nums.Split(" ");
foreach (var nstr in numbers) {
int k = Int32.Parse(nstr);
s.Append(Convert.ToChar(k));
}
return s.ToString();
}