No UTF-32 big-endian in C#?
Asked Answered
G

2

9

In C#, Encoding.UTF32 is UTF-32 little-endian, Encoding.BigEndianUnicode is UTF-16 big-endian, Encoding.Unicode is UTF-16 little-endian. But I can't find any for UTF-32 big-endian.

I'm developing a simple textviewer and don't think there are many documents encoded in UTF-32 big-endian but I want to prepare for that too, just in case.

Doesn't C# support UTF32 big-endian?

BTW Java supports it.

Gotthelf answered 6/10, 2015 at 15:23 Comment(3)
msdn.microsoft.com/en-us/library/… seems to indicate that the byte order is a parameter to the constructor -- is that something different than you had in mind?Phasis
Encoding.GetEncoding("utf-32be") is a simple way to get one.Mantling
Thanks for the informative replies both of you!Gotthelf
C
14

It does support big endian on UTF-32. Just create the encoding yourself using the overloaded constructor:

Encoding e = new UTF32Encoding(true /*bigEndian*/, true /*byteOrderMark*/);

The encodings predefined as static on Encoding aren't an exhaustive list. You can create much and much more other encodings.

Contract answered 6/10, 2015 at 15:29 Comment(2)
I found a few resources that said it could be done but not explicitly how to do it - nice work!Sword
Wow! you saved me! Thanks a lot!!Gotthelf
P
0
//https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding?view=netframework-4.7.2
//12000 utf-32  Unicode (UTF-32)    ✓   ✓
//12001 utf-32BE    Unicode (UTF-32 Big endian)
const string strUniRepChr = "�"; //Unicode Character 'REPLACEMENT CHARACTER' (U+FFFD)
Encoding cpUTF32 = Encoding.GetEncoding(12000,
                   new EncoderReplacementFallback(strUniRepChr),
                   new DecoderReplacementFallback(strUniRepChr) );
Peadar answered 20/9, 2018 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.