Home > Back-end >  Converting char to ASCII symbol
Converting char to ASCII symbol

Time:03-22

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:

  1. 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.

  2. 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.

  1. the variable s is not very descriptive. Let's rename it to helloworld, so you have a better idea of what it's being used for.

  2. 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"};
  1. We can now get rid of nums and the old numbers variables. We don't need those.

  2. The for loop gives you a string 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.

  1. 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 a char
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;
}
  •  Tags:  
  • c#
  • Related