473,788 Members | 3,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determine File Encoding

Hi there,

Can anyone point out any really obvious flaws in the methodology below
to determine the likely encoding of a file, please? I know the number
of types of encoding is small, but that is only because the
possibilities I need to work with is a small list.
private string determineFileEn coding(FileStre am strm)
{
long originalSize = strm.Length;
StreamReader rdr = new StreamReader(st rm);

strm.Position = 0;
System.Text.UTF 8Encoding unic = new System.Text.UTF 8Encoding();
byte[] inputFile = unic.GetBytes(r dr.ReadToEnd()) ;
if(inputFile.Le ngth == originalSize)
{
return "UTF8";
}

strm.Position = 0;
System.Text.Uni codeEncoding unic2 = new System.Text.Uni codeEncoding();
byte[] inputFile2 = unic2.GetBytes( rdr.ReadToEnd() );
if(inputFile2.L ength == originalSize)
{
return "Unicode";
}

strm.Position = 0;
System.Text.UTF 7Encoding unic3 = new System.Text.UTF 7Encoding();
byte[] inputFile3 = unic3.GetBytes( rdr.ReadToEnd() );
if(inputFile3.L ength == originalSize)
{
return "UTF7";
}

System.Text.ASC IIEncoding unic4 = new System.Text.ASC IIEncoding();
byte[] inputFile4 = unic3.GetBytes( rdr.ReadToEnd() );
if(inputFile4.L ength == originalSize)
{
return "Ascii";
}

return "Not known";
}


Thanks in advance
Marc.
Nov 17 '05
10 10104
Roby Eisenbraun Martins
<Ro************ *******@discuss ions.microsoft. com> wrote:
If you open a file using StreamReader it will load a CurrentEncoding
with the correct file encoding and convert the bytes to the correct
characters.


Only if you're lucky. It won't be able to guess correctly between
different ANSI character sets, for instance.

It's definitely best to take the guesswork out, either by explicitly
stating the encoding, making sure there *is* only one encoding, or
allowing the user to override any guesswork which has been performed.

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

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

Similar topics

2
6866
by: jamie | last post by:
I have a file that was generated on a customers computer which is not using the Windows default text encoding but uses Japanese(Shift-JIS) encoding. If I open the file in notepad the data looks normal but my application can not read in the data properly (Line breaks are not what is expected) Is there a way to translate a file to the windows default text encoding? Also, is there an API to determine the which encoding a system is using?
0
1768
by: Merav | last post by:
I'm running a java application from Eclipse. Looking at the system properties I get the following values: file.encoding = Cp1255 user.language = iw user.country = IL Than I'm building a jar for the application and running the jar. Looking at the system properties I get the following values: file.encoding = Cp1252 user.language = en
2
1820
by: Matthew Mueller | last post by:
I noticed in python2.3 printing unicode to an appropriate terminal actually works. But using sys.stdout.write doesn't. Ex: Python 2.3.4 (#2, May 29 2004, 03:31:27) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> sys.stdout.encoding 'UTF-8' >>> u=u'\u3053\u3093\u306b\u3061\u308f'
1
10146
by: Gaia C via .NET 247 | last post by:
Hi All, How can i found out at what encoding the file was saved? I tried usign GetPreamble(), but for this i should already create a stream which get an encoding... StreamReader sr = new StreamReader(path); sr.CurrentEncoding.GetPreamble(); any idea? Thanks,
8
7586
by: Xarky | last post by:
Hi, I am downloading a GIF file(as a mail attachement) with this file format, Content-Transfer-Encoding: base64; Now I am writing the downloaded data to a file with this technique: streamWriter = new StreamWriter(@startupPath+"\\"+filename, false); streamWriter.WriteLine(data); I am not specifying any file Encoding. When I try to open the file
0
1551
by: Thomas Podlesak | last post by:
I need a function to determine if a file is utf-8 encoded. I found the inconv- and recode-functions. But they only seem to convert files. Isn´t there any php-function similar to the 'file'-command on linux?
3
1494
by: tomislav.vujnovac | last post by:
I need to generate xml file with data from ms sql server. That xml is used in some third party java program. That program need xml file that is saved as "windows-1250" but in first line is: "<?xml version="1.0" encoding="UTF-8"?>" When I make xml file as UTF-8, I can not open it with that java program, but when I make as "windows-1250" than I can open it but I can not see CE characters Tomislav
19
2595
by: Edward K. Ream | last post by:
Following the usual cookbook examples, my app parses an open file as follows:: parser = xml.sax.make_parser() parser.setFeature(xml.sax.handler.feature_external_ges,1) # Hopefully the content handler can figure out the encoding from the <?xml>
3
2956
by: Sun | last post by:
Hi everyone . I have two files named a.txt and b.txt. I open a.txt with ultraeditor.exe. here is the first row of the file: neu für then I switch to the HEX mode: 00000000h: FF FE 6E 00 65 00 75 00 20 00 66 00 FC 00 72 00 20 00 0A 00 0D 00
0
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.