Question about UTF8 Encoding
-
How do I take an arbitrary string and UTF8 encode it using System.Text.Encoding.UTF8? I found out how to write strings and how to convert byte data, but not how to convert strings. You'd think this would be easier; maybe I'm missing something. (I'm using C# by the way).
-
How do I take an arbitrary string and UTF8 encode it using System.Text.Encoding.UTF8? I found out how to write strings and how to convert byte data, but not how to convert strings. You'd think this would be easier; maybe I'm missing something. (I'm using C# by the way).
Use Encoding class in System.Text namespace. Basically to convert, you first set up encoding. Let's say you have a simple string that you named simpleS. Here is the code that would convert: Encoding ascii = Encoding.ASCII; Encoding utf8 = Encoding.UTF8; byte[] simpleBytes = ascii.GetBytes(simpleS); byte[] newbytes = Encoding.Encode(ascii, utf8, simpleBytes); that's it. Key here is static Encode method provided by Encoding class. Once you have the new byte array, you can convert it to char array of utf8 encoded chars. I hope this is of some help. MSDN documentation also has a very similar example and more stuff on this encoding. I know I used it for the project I worked some time ago.