sa319
October 26, 2011, 2:39pm
1
Using FEZ Panda II hardware.
Below is a code fragment showing a byte array with an ASCII “O”, “K”, return, and some 0’s.
What would I need to replace the *** in the print argument below so that it would output OK.
…
byte[] rx_data = new byte[10] {79,75,13,0,0,0,0,0,0,0};
…
Debug.Print(***);
Not tested
Debug(new String(System.Text.Encoding.UTF8.GetChars(buf, 0, 3)));
sa319
October 26, 2011, 3:36pm
3
Tried several variations. Generates an error indicating “cannot implicitly convert type ‘char[]’ to ‘string’.”
There’s a GetString method shown in the MS docs for the Encoding class but it doesn’t seem to be present. I don’t know what that means.Am I missing a namespace reference or ‘using’ statement. Encoding.GetChars(byte[]) is there but it doesn’t seem to like the argument.
Mike
October 26, 2011, 3:47pm
4
[quote]Tried several variations. Generates an error indicating “cannot implicitly convert type ‘char[]’ to ‘string’.”
[/quote]
Please include the specific code that is giving you an error.
sa319
October 26, 2011, 3:57pm
5
The three last lines below were all underlined in red in the IDE starting after the ‘=’.
string OStr;
byte[] rx_data = new byte[10] {79,75,13,0,0,0,0,0,0,0};
…
OStr=System.Text.Encoding.UTF8.GetChars(rx_data);
OStr=System.Text.Encoding.UTF8.GetChars(rx_data,0,3);
OStr=System.Text.Encoding.UTF8.GetChars(rx_data[]);
Mike
October 26, 2011, 5:19pm
6
GetChars() returns a char[] not a string.
As suggested earlier:
OStr= new String(System.Text.Encoding.UTF8.GetChars(rx_data));
This works good
byte[] buf = new byte[10] { 79, 75, 13, 0, 0, 0, 0, 0, 0, 0 };
Debug.Print(new String(System.Text.Encoding.UTF8.GetChars(buf)));
sorry Mike we were replying in parallel
sa319
October 26, 2011, 8:30pm
8
Thanks guys. I see that my problem was that I never instantiated OStr (left off the ‘new’). The problem with being new to C#.