File Conversion

A

anant

is there any way to change format of any kind of file to little
endian
or big endian in c#
 
J

Jon Skeet [C# MVP]

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
 
M

Marc Gravell

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
 
M

Marc Gravell

(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)
 
M

Marc Gravell

[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top