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?

er-sho
  • 9,581
  • 2
  • 13
  • 26
Justin
  • 31
  • 4

6 Answers6

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)
}
Vibeeshan Mahadeva
  • 7,147
  • 8
  • 52
  • 102
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());
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
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

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • 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!! – Justin Sep 01 '18 at 06:27
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);
Ofir Winegarten
  • 9,215
  • 2
  • 21
  • 27
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

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
  • 1
    Good shout, just needs a bit of addition to help him out with generating that byte array? Array.ConvertAll(String.Split,byte.Parse) maybe? – Caius Jard Sep 01 '18 at 06:19
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

er-sho
  • 9,581
  • 2
  • 13
  • 26