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!
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.
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 ;)
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With