-1

I pass a string containing separate characters on each line to a Unicode list with this code.

string MultiLineCharArray = string.Join(Environment.NewLine, CharArray);
var UnicodeList = MultiLineCharArray.Select(str => Convert.ToUInt16(str)).ToList();

when reversing it the program dies, it does not even try, very badly:

for (int i = 0; i < UnicodeList.Count; i++)
{
      MultiLineCharArray = string.Join(Environment.NewLine, Convert.ToChar(UnicodeList[i]));
}

I need the MultiLineCharArray to then convert it into an Array of its valid Unicode characters (unicode = A) going through each line to convert it to a single string. The Unicode list is very long (9,000) elements, maybe that's why the program crashed, is there a more optimal way to do it?

Moix
  • 35
  • 6
  • 1
    I struggle to understand what and [why](https://meta.stackexchange.com/q/66377/147640) you are trying to do, but I can't help the feeling that most or all the steps are unnecessary. What kind of numbers do you want in the first place? If you want an array of chars' codes (why?), then why do you introduce the string with bogus newlines in it? And if you want codepoints, you are not doing it [correctly](https://stackoverflow.com/a/28155130/11683). – GSerg Sep 24 '21 at 18:20

1 Answers1

1

Use LINQ and String functions

// to populate charArray with dummy chars A through J
var charArray = Enumerable.Range(65, 10).Select(i => (char)i);
// your existing code
var multiLineCharArray = string.Join(Environment.NewLine, charArray);
var unicodeList = multiLineCharArray.Select(str => Convert.ToUInt16(str)).ToList();
// to reverse
var multiLineCharArray1 = new string(unicodeList.Select(u => (char)u).ToArray());
var charArray1 = multiLineCharArray1.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
djv
  • 15,168
  • 7
  • 48
  • 72