Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default C# String encoding

Tags:

string

c#

I am having some issues with the default string encoding in C#. I need to read strings from certain files/packets. However, these strings include characters from the 128-256 range (extended ascii), and all of these characters show up as question marks , instead of the proper character. For example, when reading a string ,it could come up as "S?meStr?n?" if the string contained the extended ascii characters.

Now, is there any way to change the default encoding for my application? I know in java you could define the default character set from command line.

like image 496
Dan Avatar asked Jul 01 '11 15:07

Dan


People also ask

What is default in C programming?

The default statement is executed if no case constant-expression value is equal to the value of expression . If there's no default statement, and no case match is found, none of the statements in the switch body get executed. There can be at most one default statement.

Why is default used in C?

2) Default: This keyword is used to specify the set of statements to execute if there is no case match. Note: Sometimes when default is not placed at the end of switch case program, we should use break statement with the default case. 2) Duplicate case values are not allowed. 3) The default statement is optional.

Does default Need Break C?

A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Is default a keyword in C?

if, else, switch, case, default – Used for decision control programming structure. break – Used with any loop OR switch case. int, float, char, double, long – These are the data types and used during variable declaration.


2 Answers

There's no one single "extended ASCII" encoding. There are lots of different 8-bit encodings which are compatible with ASCII for the bottom 128 values.

You need to find out what encoding your files actually use, and specific that when reading the data with StreamReader (or whatever else you're using). For example, you may want encoding Windows-1252:

Encoding encoding = Encoding.GetEncoding(1252);

.NET strings are always sequences of UTF-16 code points. You can't change that, and you shouldn't try. (That's true in Java as well, and you really shouldn't use the platform default encoding when calling getBytes() etc unless that's what you really, really mean.)

like image 78
Jon Skeet Avatar answered Sep 23 '22 13:09

Jon Skeet


An Encoding can be specified in at least one overload of functions for reading text - for example, ReadAllText(string, Encoding).

So if you no a file's encoded using Windows-1252, then you can specify it like so:

string contents = File.ReadAllText(someFilePath, Encoding.GetEncoding(1252));

Of course, doing this requires knowing ahead of time which code page is being used.

like image 40
Sean U Avatar answered Sep 21 '22 13:09

Sean U