Reputation: 31
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?
Upvotes: 1
Views: 1177
Reputation: 19106
Straight forward:
var data = new byte[]{104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100};
var str = Encoding.ASCII.GetString(data);
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);
Upvotes: 0
Reputation: 9771
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:
Upvotes: 0
Reputation: 74605
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
Upvotes: 1
Reputation: 7238
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)
}
Upvotes: 1
Reputation: 29836
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());
Upvotes: 1
Reputation: 9365
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);
Upvotes: 0