473,385 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Encoding problem

Hi,

I'm trying to read in a file written in French.

StreamReader sr = new StreamReader(fn,System.Text.Encoding.UTF8);
string s = sr.ReadToEnd();

I can edit the file fn in VS and it looks fine. The variable s
receives all the data except characters with accents.

"Déclaration" appears with out the accented e. (may not display on
your browser)
Same thing with umlauted chars in a German file.

If I use the form:

StreamReader sr = new StreamReader(fn);

I can break after the statement and see that sr.CurrentEncoding is
UTF8. Still no luck with getting all of the string.

Must be missing something, but what?

Thanks!
Nov 15 '05 #1
8 6443
MarkMurphy <mu****@murphysw.com> wrote:
I'm trying to read in a file written in French.
But in which encoding? You can't just assume it's UTF-8 if it's not -
do you have any reason to believe the file is in UTF-8?

<snip>
If I use the form:

StreamReader sr = new StreamReader(fn);

I can break after the statement and see that sr.CurrentEncoding is
UTF8.


That's because that's the default encoding if you don't specify one.

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information about this kind of thing.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2
If you open the file in VS7 and do select File | Adanced Save Options it
will tell you what the real encoding is.

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
MarkMurphy <mu****@murphysw.com> wrote:
I'm trying to read in a file written in French.


But in which encoding? You can't just assume it's UTF-8 if it's not -
do you have any reason to believe the file is in UTF-8?

<snip>
If I use the form:

StreamReader sr = new StreamReader(fn);

I can break after the statement and see that sr.CurrentEncoding is
UTF8.


That's because that's the default encoding if you don't specify one.

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information about this kind of thing.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #3
john farrow <vi*******@xtra.co.nz> wrote:
If you open the file in VS7 and do select File | Adanced Save Options it
will tell you what the real encoding is.


I don't see how it can, to be honest. It can make a *guess* as to what
the encoding is, but surely it can't tell the difference between two
identical files representing different content in different encodings.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #4
john farrow <vi*******@xtra.co.nz> wrote:
How could I have "identical files representing different content in
different encodings".
The files would contain the same bytes, but represent different content
in different encodings.
If the encoding was different they would not be identical.


Not if they have different content.

Here's an example. Two files, first is utf8.txt, which represents
characters ab (Unicode 0x0123, latin small letter g with cedilla) cd
That file is encoded in UTF-8 (without BOM) and the bytes in it are (in
hex):

61 62 C4 A3 63 64

The second file is called cp1252.txt, which contains "abģcd" with an
encoding of code page 1252. (The middle two characters are a capital A
with an umlaut and a pounds-sterling sign, in case it doesn't show up
on your newsreader.) That file (in hex) is:

61 62 C4 A3 63 64

There we have it: identical files (same binary content) representing
different content (the two different strings) in different encodings
(UTF-8 and CP1252).

Open either file with VS.NET and it's got to guess which encoding the
file is meant to be in - and in one of the cases, it's bound to be
wrong.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #5
Hi Guys,

If you save a text file in NotePad as UTF-8 it sticks the
characters 0xEF 0xBB at the front. For Unicode it's 0xFF 0xEE. For
ANSI it doesn't have anything.

I tried the same exercise in VS, selecting encodings with
File\Advanced Save Options. It didn't stick in the encoding.

Who knows?

Regards,
Fergus
Nov 15 '05 #6
Fergus Cooney <fi******@tesco.net> wrote:
If you save a text file in NotePad as UTF-8 it sticks the
characters 0xEF 0xBB at the front. For Unicode it's 0xFF 0xEE. For
ANSI it doesn't have anything.
Sure - that's what notepad does, but not everything does.
I tried the same exercise in VS, selecting encodings with
File\Advanced Save Options. It didn't stick in the encoding.

Who knows?


I know - it's ambiguous, basically.

I picked UTF-8 and CP1252 almost at random. There are other simple
examples - two different simple 8-bit Windows code pages would provide
exactly the same kind of behaviour (different content and different
encodings potentially producing identical files), and there's not even
the possibility of byte ordering marks there.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #7
Thanks all for the enlightening discussion. I have this working now
and here is understanding of the solution.

It appears that VS saves files by default in Western European
(Windows) Codepage 1252, not UTF-8. I had been assuming UTF-8 and
when I loaded the file into a StreamReader and checked the value of
the CurrentEncoding, it was displayed as UTF-8 in the debugger.
Perhaps the encoding could not be determined and defaulted to Western
European.

In any case, we try to store localized files as UTF-8 and so I did so
by doing a 'save as' and selecting the down arrow next to the Save
button. This allows me to save with encoding of Unicode (UTF-8) with
signature. After doing that, those exotic umlauts and accents began
to appear in my string read in by StreamReader.

VS help warns that UTF-8 is not supported by Visual SourceSafe in text
mode. Binary is. I tried checking in some text UTF-8 files and
checking them back out without trouble. Perhaps problems arise when
doing compares and other functions.
Nov 15 '05 #8
MarkMurphy <mu****@murphysw.com> wrote:
Thanks all for the enlightening discussion. I have this working now
and here is understanding of the solution.

It appears that VS saves files by default in Western European
(Windows) Codepage 1252, not UTF-8. I had been assuming UTF-8 and
when I loaded the file into a StreamReader and checked the value of
the CurrentEncoding, it was displayed as UTF-8 in the debugger.
Perhaps the encoding could not be determined and defaulted to Western
European.
No, StreamReader doesn't attempt to work out the encoding. It uses
UTF-8 if you don't specify which encoding to use. (See MSDN for more
info.)
In any case, we try to store localized files as UTF-8 and so I did so
by doing a 'save as' and selecting the down arrow next to the Save
button. This allows me to save with encoding of Unicode (UTF-8) with
signature. After doing that, those exotic umlauts and accents began
to appear in my string read in by StreamReader.


Good good. Alternatively, you could use CP1252 in the StreamReader...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ann | last post by:
Hi, Is there any way to Change encoding of Java Vm to ISO-8859-1? i am using Java vm along with an application called opencms. I get the following error message.. Error: the encoding of your...
8
by: janeaustine50 | last post by:
Python's InteractiveInterpreter uses the built-in compile function. According to the ref. manual, it doesn't seem to concern about the encoding of the source string. When I hand in an unicode...
7
by: Mark | last post by:
Hi... I've been doing a lot of work both creating and consuming web services, and I notice there seems to be a discontinuity between a number of the different cogs in the wheel centering around...
8
by: davisjoseph | last post by:
Hi All, I'm newbie to this XML world. My problem is to identify the encoding type of XML at runtime. What currently I'm doing is checking whether BOM is available in the XML; based on the BOM...
8
by: Demon News | last post by:
I'm trying to do a transform (Using XmlTransform class in c#) and in the Transform I'm specifying the the output xsl below: <xsl:output method="xml" encoding="UTF-8" indent="no"/> the...
4
by: fitsch | last post by:
Hi, I am trying to write a generic RSS/Atom/OPML feed client. The problem is, that those xml feeds may have different encodings: - <?xml version="1.0" encoding="ISO-8859-1" ?>... - <?xml...
5
by: James Wong | last post by:
Dear all, I've a web service function and it contains a parameter in System.Text.Encoding. I found that the data type of this parameter in caller application becomes MyWebSvcName.Encoding...
0
by: Janusz Nykiel | last post by:
I've stumbled upon unexpected behavior of the .NET 2.0 System.Xml.XmlWriter class when using it to write data to a binary stream (System.IO.Stream). If the amount of data is less than a certain...
3
by: Martin Z | last post by:
Hi, I have an application that involves sending a lot of XML data to various places. The problem is that once in a while, I just want the XML document as a string (for example, sending to a...
23
by: Allan Ebdrup | last post by:
I hava an ajax web application where i hvae problems with UTF-8 encoding oc chineese chars. My Ajax webapplication runs in a HTML page that is UTF-8 Encoded. I copy and paste some chineese chars...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.