Main Site Documentation

Simple conversion


#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(***);


#2

Not tested

Debug(new String(System.Text.Encoding.UTF8.GetChars(buf, 0, 3)));


#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.


#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.


#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[]);


#6

GetChars() returns a char[] not a string.

As suggested earlier:

OStr= new String(System.Text.Encoding.UTF8.GetChars(rx_data));


#7

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 :slight_smile:


#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#.