converting an array of bytes to an array of chars

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
 
J

Jon Skeet [C# MVP]

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(...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top