Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

File Conversion

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
anant - 12 Mar 2008 09:54 GMT
is there any way to change format of any kind of file to little
endian
or big endian in c#
Jon Skeet [C# MVP] - 12 Mar 2008 10:01 GMT
> is there any way to change format of any kind of file to little
> endian or big endian in c#

Well, you would have to know the format of the file, so you would know
whether to read sequences of Int16, Int32 etc.

However, I have some classes which may help you - they're like
BinaryReader and BinaryWriter, but you can specify the endianness.

See http://pobox.com/~skeet/csharp/miscutil

Jon
anant - 12 Mar 2008 10:08 GMT
thanks...

> > is there any way to change format of any kind of file to little
> > endian or big endian in c#
[quoted text clipped - 8 lines]
>
> Jon
Marc Gravell - 12 Mar 2008 10:04 GMT
No. To change the endianness, you need to understand the contents to know
where each cluster of bytes begins and ends - for example, if the file
contains 4 byes of Int32 "foo" and 2 bytes of Int16 "bar" - then without
prior knowledge you don't know how many bytes to reverse.

If you are just worrying about text files and UTF-16 then life is simpler -
you can simply read and write the file specifying the desired Encodings :
Encoding.Unicode and Encoding.BigEndianUnicode should do the job.

Marc
Marc Gravell - 12 Mar 2008 10:09 GMT
(note - you can of course change UTF-16 endianness more directly, but the
approach cited applies to all other encodings, where-as a direct byte swap
only works for encodings such as UTF-16; plus you don't need to worry about
the BOM etc)
Marc Gravell - 12 Mar 2008 13:28 GMT
[OP, via e-mail]
> i have text file only which i want to  convert in specified format
> will this Encoding.Unicode and Encoding.BigEndianUnicode do the job .?

Yes, assuming that your specified format is UTF-16. It should work
with any other available encoding that you care to name. For
reasonable-length files you can probably even get awasy with simply:

           string contents = File.ReadAllText(path,
Encoding.Unicode);
           File.WriteAllText(path, contents,
Encoding.BigEndianUnicode);

(and even the first line might be considered overkill, as it will
*usually* guess the incoming encoding)

Marc

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.