Created
March 28, 2021 11:10
-
-
Save Meigyoku-Thmn/fc68c4bf52fa4ec7beabaa7879de9588 to your computer and use it in GitHub Desktop.
C# UTF8 Encoding with CP1252 Fallback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DecoderCP1252Fallback : DecoderFallback | |
{ | |
public override DecoderFallbackBuffer CreateFallbackBuffer() | |
{ | |
return new DecoderCP1252FallbackBuffer(); | |
} | |
public override int MaxCharCount { | |
get { | |
return 1; | |
} | |
} | |
} | |
public class DecoderCP1252FallbackBuffer : DecoderFallbackBuffer | |
{ | |
readonly Encoding cp1252 = Encoding.GetEncoding(1252); | |
byte[] bytesUnknown; | |
int fallbackCount = -1; | |
int fallbackIndex = -1; | |
public override void Reset() | |
{ | |
fallbackCount = -1; | |
fallbackIndex = -1; | |
bytesUnknown = null; | |
} | |
// Fallback Methods | |
public override bool Fallback(byte[] bytesUnknown, int index) | |
{ | |
Reset(); | |
this.bytesUnknown = bytesUnknown; | |
fallbackCount = bytesUnknown.Length; | |
fallbackIndex = -1; | |
return true; | |
} | |
public override char GetNextChar() | |
{ | |
fallbackCount--; | |
fallbackIndex++; | |
if (fallbackCount < 0) | |
return '\0'; | |
if (fallbackCount == int.MaxValue) | |
{ | |
fallbackCount = -1; | |
return '\0'; | |
} | |
return cp1252.GetString(bytesUnknown, fallbackIndex, 1)[0]; | |
} | |
public override bool MovePrevious() | |
{ | |
if (fallbackCount >= -1 && fallbackIndex >= 0) | |
{ | |
fallbackIndex--; | |
fallbackCount++; | |
return true; | |
} | |
return false; | |
} | |
public override int Remaining => (fallbackCount < 0) ? 0 : fallbackCount; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); | |
var utf8Encoding = Encoding.GetEncoding("utf-8", Encoding.UTF8.EncoderFallback, new DecoderCP1252Fallback()); | |
var test = utf8Encoding.GetString(new byte[] { 0x76, 0xF3, 0xF3, 0x72 }); | |
Console.OutputEncoding = Encoding.Unicode; | |
Console.WriteLine(test); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment