Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET internal Encoding

what is the internal encoding from .NET applications?(for example string objects) Can I define what encoding my appliction should use? If i write a .net string to a file. What encoding has the string?

//edit

Dim test as String="Das ist ein Test" <---what Encoding has this String?

Dim reader as New IO.StreamReader(docPath, _
    System.Text.Encoding.GetEncoding("shift-jis"))

test=reader.ReadToEnd() <---and now? What Encoding has this String?

Thank you!

like image 606
Mimefilt Avatar asked Nov 11 '10 14:11

Mimefilt


4 Answers

Dim test as String="Das ist ein Test" <---what Encoding has this String?

UTF-16

Dim reader as New IO.StreamReader(docPath,
    System.Text.Encoding.GetEncoding("shift-jis"))
test=reader.ReadToEnd <---and now? What Encoding has this String?

Still UTF-16. The StreamReader class looks at the bytes in docPath and converts them to UTF-16 based on the shift-jis encoding.

like image 115
Tim Robinson Avatar answered Oct 22 '22 20:10

Tim Robinson


System.String is UTF-16. You can convert that to various other encodings using derivatives of the System.Text.Encoding class.

In response to edit: System.IO.StreamReader, as far as I am aware tries to "guess" as to the correct encoding if one is not specified. System.IO.StreamWriter writes as UTF-8, IIRC. I'm less familiar with these classes so take that information at your own risk ;)

like image 29
Billy ONeal Avatar answered Oct 22 '22 20:10

Billy ONeal


As all of the other answers: yes, 2 byte Unicode (UTF-16). And yes, you can control how it writes to disc, like described by @Billy ONeal.

Concerning your question whether it is possible to control this: No, this is not possible. .NET will always run on Unicode UTF-16 internally. There are no settings for this.

like image 24
Pieter van Ginkel Avatar answered Oct 22 '22 20:10

Pieter van Ginkel


Internally .NET uses Unicode --UPDATED-- UTF-16.

However, if you write the string to a file, you have to provide an encoding. If you don't .NET will choose an encoding for you - this is usually UTF8. Here is reflectored File.WriteAllText:

public static void WriteAllText(string path, string contents)
{
    if (path == null)
    {
        throw new ArgumentNullException("path");
    }
    if (path.Length == 0)
    {
        throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
    }
    InternalWriteAllText(path, contents, StreamWriter.UTF8NoBOM);
}
like image 40
Aliostad Avatar answered Oct 22 '22 18:10

Aliostad