Convert char to byte

Hi

if I wrote

char c = some value and put in consideration UTF8;

byte b = (char)c;

What chances that I will loose data? and how can I convert without lossing data

Thanks

[308 byte] By [C#Developer] at [2008-1-8]
# 1

Hi,

A char is 16bit, byte 8 bit. You will loose data if you use this code.

Check these interesting posts to understand it better

http://discuss.joelonsoftware.com/default.asp?joel.3.151416.13

http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=35689

HTH,

Suprotim Agarwal

SuprotimAgarwal at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2
Suprotim is right. You'll need a byte array:

byte[] b = System.Text.Encoding.UTF8.GetBytes(new char[] { c });

The array will contain between 1 and 4 bytes.

nobugz at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified