Register  |  Login




Advertisement

Start Your Own Q&A Site

Create your own Q&A site easily, allowing you to quickly grow a new community around any subject matter or generate new organic traffic for your existing website.

Question

Status: Closed Points: 60 Time: 05:25 - Apr 07, 2007  

dustPuppy

how do i detect the encoding of a text file?

I have a text file (.txt) which apparently is not UTF-8 encoded.
When I read it in C#.net with a streamreader, some characters are lost like "Ç".
When I open the file with notepad, the character is there.
So I suppose that it's some other encoding, but I don't know how to detect it?

Answer Discussion
Tutorials

 

PeterNZ

Date:: Apr 09, 2007

Time:: 17:15

Have a look at this article. This might help you:

http://www.codeproject.com/useritems/Det...

Cheers

Peter

PeterNZ

Date:: Apr 09, 2007

Time:: 17:15

Sorry, here is another one:
http://www.devhood.com/tutorials/tutoria...

Cheers

Peter

dustPuppy

Date:: Apr 11, 2007

Time:: 23:37

Thanks for the link, Peter.
I was actually looking for a generic, not programming way of detecting the file encoding, but I didn't find that yet.
What I did find by testing with different encodings was that my text was UTF-7.

So where I first was doing:
StreamReader file = File.OpenText(fullfilename);

I had to change it to:
StreamReader file = new StreamReader(fullfilename, System.Text.Encoding.UTF7);

OpenText assumes it's UTF-8.

you can also create the StreamReader like this
new StreamReader(fullfilename, true), the second parameter meaning that it should try and detect the encoding from the byteordermark of the file, but that didn't work in my case.

PeterNZ

Date:: Apr 12, 2007

Time:: 14:45

Yes, I know what you mean, something like

if(file.Encoding == System.Text.Encoding.UTF7)
{
[...]
}

Would be nice. Well, maybe in.Net Framework 4.0?

Cheers for the points!

Peter

Prajith

Date:: Jul 24, 2007

Time:: 12:52

http://msdn2.microsoft.com/en-us/netfram...

magusmao

Date:: Jan 16, 2009

Time:: 20:49

file -i <filename>

Question Answered

This question has been closed, and points have been rewarded to the following experts:


PeterNZ: 30
dustPuppy: 30

You're welcome however to comment or give additional information or if you wish, you have the ability to write a Tutorial in the Tutorial Area.

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

No tutorials have been submitted yet. Want to be the first?

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

Ask a Question

Have a new question? Ask!

You have 100 characters to use



Top Experts

View More

Rank

Expert

Points

1.

nidhi

10354

2.

oracleofDelphi

6493

3.

rcastagna

5596

4.

LAGM

4848

5.

PeterNZ

3487

6.

gonzalo

2840

7.

Mason

2770

8.

jgivoni

2303

9.

xarcus

1820

10.

Anpanman

917

Become an Expert

Register today to share your knowledge with the community and be recognized and rewarded for your contributions.


Register Here




"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it



Language Options

English:

www.quomon.com

Español:

www.quomon.es