1

Essentially, I'm trying to do the same thing as the Python code below, just in C#.

A user will provide a long list of ASCII values separated by commas, e.g., "104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100" and I want to convert them to readable text and return that information to a text box. Feel like I'm making this out to be way more complicated than it should be Any help is greatly appreciated, thanks!

decodeText = userInputBox.Text;
var arr = new string[] { decodeText};

int[] myInts = Array.ConvertAll(arr, int.Parse);

for(int i = 0; i < myInts.Length; i++)
{
    decodeBox.Text = not sure??
}

Python sample:

L = [104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100] ''.join(map(chr,L)) 'hello, world'

How do I convert a list of ascii values to a string in python?

6 Answers 6

1

You need to split the input by , then parse the string number into a real number and cast it into a char and then create a string using it's char[] ctor.

var result = new string(userInputBox.Text.Split(',').Select(c => (char)int.Parse(c)).ToArray());
Sign up to request clarification or add additional context in comments.

Comments

1
String finalString = String.Empty;

foreach(var item in myInts)
{
   int unicode = item;
   char character = (char) unicode;
   string text = character.ToString();
   finalString += text;
}

If you want a little bit of performance gain using String Builder.

StringBuilder finalString = new StringBuilder();

foreach(var item in myInts)
{
   char character = (char) item;
   builder.Append(character)
}

3 Comments

Did you avoid use of StringBuilder for a particular reason?
No, I didn't optimize the performance. Just wanted to make a code much easily understandable.
@VibeeshanRC Awesome! This is actually what I needed! I really appreciate you making the code very understandable, still new to C#. This will help me with other concepts as well. Thanks!
1

I do believe you were a good way there with your first attempt:

var arr = userInputBox.Text.Split(‘,’);
textbox.Text = new string(Array.ConvertAll(arr, s => (char)(int.Parse(s)));

Where you used convertall to change an array of string to an array of int, I added a cast to char to make it output an array of chars instead (I explain why, below) and this can be converted to a string by passing it into a new string’s constructor

This has resulted in a much more compact (and I could have make it a one liner to be honest) form but compactness isn’t always desirable from a learning perspective

Hence, this is fixing your code if it’s easier to understand:

decodeText = userInputBox.Text;
var arr = decodeText.Split(‘,’);

int[] myInts = Array.ConvertAll(arr, int.Parse);

for(int i = 0; i < myInts.Length; i++)
{
    decodeBox.Text += (char)myInts[i];
}

The crucial bit you were missing (apart from using string split to split the string into an array of numerical strings) was converting the int to a char

In c# there is a direct mapping from int to char- letter A is 65, for example and if you take the int 65 and cast it to a char, with (char) it becomes A

Then we just append this

If the list of ints will be long, consider using a stringbuilder to build your string

1 Comment

Thanks for the in-depth explanation! I didn't realize how close I was, but you've helped me understand significantly more about how everything is working and better ways to do it. I've upvoted (still low rep, so it won't show publicly). Thanks again!!
0

Once you have an array of ints:

int[] asciis = new []{104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100};

its easy to transform them to an array of chars with Linq:

var chars = asciis.Select(i=>(char)i).ToArray();

And then to the string representation (luckily, we have a string constructor that does it):

var text = new string (chars);

Comments

0

Since you are just entered in c#, so you need a code that you can understand.

public class Program
{
    static void Main(string[] args)
    {
        string input = "104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100";

        String output = String.Empty;

        foreach (var item in input.Split(',').ToArray())
        {
            int unicode = Convert.ToInt32(item);
            var converted = char.ConvertFromUtf32(unicode);
            output += converted;
        }

        Console.WriteLine(output);
        Console.ReadLine();
    }
}

ConvertFromUtf32(int utf32): Converts the specified Unicode code point into a UTF-16 encoded string.

Output:

enter image description here

1 Comment

@Justin, view the answer might be it help you :)
0

Straight forward:

var data = new byte[]{104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100};
var str = Encoding.ASCII.GetString(data);

.net fiddle sample

If you need to convert the data from a text input you need to convert that

var input = "104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100";
var data = Array.ConvertAll( 
    input
        .Split(',')
        .Select( e => e.Trim() )
        .ToArray(),
    Byte.Parse );
var str = Encoding.ASCII.GetString(data);

.net fiddle sample

1 Comment

Good shout, just needs a bit of addition to help him out with generating that byte array? Array.ConvertAll(String.Split,byte.Parse) maybe?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.