Unicode Character

R

Ryu

Is there a way to get the convert a unicode character such as chinese
characters to byes? Everytime when I use Convert.toByte I will always get a
System.OverflowException "Value was either too big ot too small". Is it
because unicode characters require 16 bits? In that case how to I convert
the character to byes?
 
M

Morten Wennevik

Hi Ryu,

Yes, unicode characters are two bytes in length and cannot be stored in a
single byte.
You can convert them to two bytes using

char c = (char)22345; // random character that may not even exist
byte[] b = Encoding.Unicode.GetBytes(new char[]{c});

An overload of GetBytes takes entire string and converts it to a byte
array.
 

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