Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Converting char to ASCII symbol

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?)"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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();
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading