469,625 Members | 1,591 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

ASCII or Unicode

Can anyone provide a quick code snippit to open a text file and tell if it's
ASCII or Unicode?

Nov 21 '05 #1
1 1483
Hi Scott
You would be using the System.Text.Encoding Namespace to check for that
System.Text.Encoding enc = null;
System.IO.FileStream file = new System.IO.FileStream(filePath,
FileMode.Open, FileAccess.Read, FileShare.Read);
if (file.CanSeek)
byte[] bom = new byte[4]; // Get the byte-order mark, if there is one
file.Read(bom, 0, 4);
if ((bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) || // utf-8
(bom[0] == 0xff && bom[1] == 0xfe) || // ucs-2le, ucs-4le, and
(bom[0] == 0xfe && bom[1] == 0xff) || // utf-16 and ucs-2
(bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff))
// ucs-4
enc = System.Text.Encoding.Unicode;
enc = System.Text.Encoding.ASCII;

// Now reposition the file cursor back to the start of the file
file.Seek(0, System.IO.SeekOrigin.Begin);
// The file cannot be randomly accessed, so you need to decide what to
set the default to
// based on the data provided. If you're expecting data from a lot of
older applications,
// default your encoding to Encoding.ASCII. If you're expecting data
from a lot of newer
// applications, default your encoding to Encoding.Unicode. Also, since
binary files are
// single byte-based, so you will want to use Encoding.ASCII, even
though you'll probably
// never need to use the encoding then since the Encoding classes are
really meant to get
// strings from the byte array that is the file.

enc = System.Text.Encoding.ASCII;

// Do your file operations here, such as getting a string from the byte
array that is the file
byte[] buffer = new byte[4096]; // A good buffer size; should always be
base2 in case of Unicode
while (file.Read(buffer, 0, 4096))
string line = enc.GetString(buffer); // Uses the encoding we defined

// Close the file: never forget this step!

here you are the link to the complete article

Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Marian Aldenhövel | last post: by
4 posts views Thread by webdev | last post: by
24 posts views Thread by ChaosKCW | last post: by
19 posts views Thread by many_years_after | last post: by
2 posts views Thread by joakim.hove | last post: by
19 posts views Thread by Thomas W | last post: by
399 posts views Thread by =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.