> 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
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