converting an array of bytes to an array of chars

  • Thread starter Thread starter Claire
  • Start date Start date
C

Claire

I need to convert an array of bytes into an array of chars so I can
initialize a string without using unsafe methods and I thought of using the
following overload of the constructor. public String(char[]);

private byte[] TempBuff = new byte[Buffsize];

// read in bytes from serial port and validate.

String str = new String((char[])TempBuff); << compiler doesn't like this


how do I do this please?

thanks
 
Claire said:
I need to convert an array of bytes into an array of chars so I can
initialize a string without using unsafe methods and I thought of using the
following overload of the constructor. public String(char[]);

private byte[] TempBuff = new byte[Buffsize];

// read in bytes from serial port and validate.

String str = new String((char[])TempBuff); << compiler doesn't like this

No, I'm not surprised - you can't just cast from a byte array to a char
array.
how do I do this please?

See http://www.pobox.com/~skeet/csharp/unicode.html

Basically you need to work out which Encoding you need, and use
Encoding.GetString(...)
 
Back
Top