简体   繁体   中英

C# Encoding.CreateTranscodingStream does not output file with desired target encoding

I have the following piece of code:

using (Stream inputFileStream = File.OpenRead("C:\\Users\\User\\Downloads\\test.txt"))
{
    using (Stream transcodingStream = Encoding.CreateTranscodingStream(inputFileStream, Encoding.GetEncoding(500), new UnicodeEncoding(bigEndian: true, byteOrderMark: true)))
    {
        using (Stream outputStream = File.OpenWrite("C:\\Users\\User\\Downloads\\test.txt"))
        {
            await transcodingStream.CopyToAsync(outputStream, cancellationToken);
        }
    }
}

My file before transcoding has the following first 16 bytes and is of Ebcdic type encoding (code page 500):

F5 F1 F1 F0 F2 C2 D4 E6 40 40 40 40 40 40 F1 F1 = 51102BMW 11

After performing the transcoding to Unicode with Big-Endiant and Byte Order Markings, I expect the file to begin with:

FF FE

However, I get:

00 35 00 31 00 31 00 30 00 32 00 42 00 4D 00 57 = �5�1�1�0�2�B�M�W

Where am I going wrong with this?

It seems like the transcoding stream does not care about maintaining the BOM for target encoding and it's something you have to manage yourself.

I've implemented the following solution: targetEncoding is of type Encoding

outputStream.Seek(0, SeekOrigin.Begin);
await outputStream.WriteAsync(targetEncoding.Preamble.ToArray(), 0, targetEncoding.Preamble.Length, cancellationToken);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM