More conversion problems... char[].ToString()

These conversions are making me feel stupid, and making me miss the full framework I am so used to doing my code in.

I am trying to take a char array and turn it into a string, but I am not getting the right results. I have verified that the char array is populated validly, but I’m not sure how to get this to work right.

data is a byte array from a file and content is getting populated with my expected characters.


            char[] content = System.Text.Encoding.UTF8.GetChars(data);
            return content.ToString();

returns “System.Char[]”.


            char[] content = System.Text.Encoding.UTF8.GetChars(data);
            return new string(content);

returns null.

Any suggestions?

Thanks for all the help.

Ok, apparently I have to not trust the debug window. I coded in the “new string” and it returned correctly.

Here are debug results:

?content
{char[83]}
[0]: 53 ‘5’
[1]: 99 ‘c’
[2]: 45 ‘-’
[3]: 56 ‘8’
[4]: 54 ‘6’
[5]: 45 ‘-’
[6]: 52 ‘4’
[7]: 97 ‘a’
[8]: 45 ‘-’
[9]: 48 ‘0’
[10]: 48 ‘0’
[11]: 45 ‘-’
[12]: 48 ‘0’
[13]: 48 ‘0’
[14]: 45 ‘-’
[15]: 100 ‘d’
[16]: 102 ‘f’
[17]: 13 ‘\r’
[18]: 10 ‘\n’
[19]: 49 ‘1’
[20]: 57 ‘9’
[21]: 50 ‘2’
[22]: 46 ‘.’
[23]: 49 ‘1’
[24]: 54 ‘6’
[25]: 56 ‘8’
[26]: 46 ‘.’
[27]: 50 ‘2’
[28]: 53 ‘5’
[29]: 49 ‘1’
[30]: 46 ‘.’
[31]: 55 ‘7’
[32]: 13 ‘\r’
[33]: 10 ‘\n’
[34]: 49 ‘1’
[35]: 57 ‘9’
[36]: 50 ‘2’
[37]: 46 ‘.’
[38]: 49 ‘1’
[39]: 54 ‘6’
[40]: 56 ‘8’
[41]: 46 ‘.’
[42]: 50 ‘2’
[43]: 53 ‘5’
[44]: 49 ‘1’
[45]: 46 ‘.’
[46]: 49 ‘1’
[47]: 13 ‘\r’
[48]: 10 ‘\n’
[49]: 49 ‘1’
[50]: 57 ‘9’
[51]: 50 ‘2’
[52]: 46 ‘.’
[53]: 49 ‘1’
[54]: 54 ‘6’
[55]: 56 ‘8’
[56]: 46 ‘.’
[57]: 50 ‘2’
[58]: 53 ‘5’
[59]: 49 ‘1’
[60]: 46 ‘.’
[61]: 49 ‘1’
[62]: 51 ‘3’
[63]: 13 ‘\r’
[64]: 10 ‘\n’
[65]: 49 ‘1’
[66]: 57 ‘9’
[67]: 50 ‘2’
[68]: 46 ‘.’
[69]: 49 ‘1’
[70]: 54 ‘6’
[71]: 56 ‘8’
[72]: 46 ‘.’
[73]: 50 ‘2’
[74]: 53 ‘5’
[75]: 49 ‘1’
[76]: 46 ‘.’
[77]: 49 ‘1’
[78]: 52 ‘4’
[79]: 13 ‘\r’
[80]: 10 ‘\n’
[81]: 56 ‘8’
[82]: 50 ‘2’
?content.GetType().ToString()
“System.Char[]”
?new string(content)
null

Debug.Print works correctly, but I tried the immediate window with the second line and that gives a null. Seems to be something with doing it in the immediate window. I frequently try alternative code in the debug window when I hit an error as a way of quickly seeing if it will work or not, guess I can’t do that with Gadgeteer like I could with the full framework.

In the immediate window when I have a breakpoint after the first line
?new string(System.Text.Encoding.UTF8.GetChars(test))
returns null

Just seems to be some discrepancy with the immediate window.