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?)"
CodePudding user response:
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();
}
CodePudding user response:
public static string greet()
{
string nums = "104 101 108 108 111 32 119 111 114 108 100 33";
var bytes = nums.Split().Select(n => byte.Parse(n)).ToArray();
return Encoding.ASCII.GetChars(bytes);
}
CodePudding user response:
Quite creative, but it seems that the level of creativity does not match the level of your C# knowledge yet... There are many misunderstandings, which makes answering this question a bit hard.
Let's start in the Main()
method:
you don't use the variable
List<string> imie = new List<string>();
but actually, that type of list would be useful in a different place of the program. For the moment, let's put this line of code inside the
greet()
method instead.you call
greet()
which returns a string, but you never use the return value. Let's surround this by a print statement:Console.WriteLine(greet());
The Main() method now looks like
public static void Main()
{
Console.WriteLine(greet());
}
Let's go on with the greet()
method.
the variable
s
is not very descriptive. Let's rename it tohelloworld
, so you have a better idea of what it's being used for.Instead of using a single string, let's take the idea of having a list of strings instead.
List<string> numbers = new List<string>{"104", "101", "108", "108", "111", "32", "119", "111", "114", "108", "100", "33"};
We can now get rid of
nums
and the oldnumbers
variables. We don't need those.The
for
loop gives you astring
instead of single characters (which would have been individual digits of the numbers actually). Let's also change the variable name.
foreach (string number in numbers)
It's good practice to have singular and plural in for loops.
- For the string concatenation, let's use
int.Parse()
instead of further messing with individual digits of a character. In order for the number to become a character, we need to cast it to achar
helloworld = (char) int.Parse(number);
The method:
public static string greet()
{
List<string> numbers = new List<string>{"104", "101", "108", "108", "111", "32", "119", "111", "114", "108", "100", "33"};
string helloworld = "";
foreach (string number in numbers)
{
helloworld = (char) int.Parse(number);
}
return helloworld;
}