I'm using json.net to read data sent in json format from a server. The server encodes all string-type data it sends in json as utf-8.
Now to read the data in c# I do something like this: string s = json.Value<string>("data");
I assume the string s is now in utf-8 format, whereas the default encoding for strings in c# is utf-16 (unicode).
To convert the string to unicode, would this be correct?
byte[] bytes = Encoding.Unicode.GetBytes(s);
string unicode = Encoding.UTF8.GetString(bytes);
What I want (I think) is the raw bytes from s and then pass that to the utf-8 decoder to get unicode, but I'm not sure what exactly Encoding.Unicode.GetBytes gives me, or what I should use instead.
Encoding.UTF8.GetBytes(s)and thenEncoding.Unicode.GetString(bytes). This way you will convert the UTF8 to Unicode.json-- how does that get populated? Is there some kind of stream being read from a web response? If so, you want to passEncoding.UTF8to the stream reader.Encoding.Default.GetStringwhich isn't exactly optimal. UsingEncoding.UTF8there directly should fix all problems with utf-8 encoded strings.