本文整理汇总了C#中Encoding.GetDecoder方法的典型用法代码示例。如果您正苦于以下问题:C# Encoding.GetDecoder方法的具体用法?C# Encoding.GetDecoder怎么用?C# Encoding.GetDecoder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Encoding
的用法示例。
在下文中一共展示了Encoding.GetDecoder方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DecodeContent
public static string DecodeContent(byte[] byteInput, Encoding enc)
{
Decoder decoder = enc.GetDecoder();
char[] chars = new char[byteInput.Length];
byte[] bytes = new byte[byteInput.Length * 4];
bool completed = false;
int byteIndex = 0;
int bytesUsed;
int charsUsed;
bool flush = false;
decoder.Convert(byteInput, byteIndex, byteInput.Length,
chars, 0, byteInput.Length, flush,
out bytesUsed, out charsUsed, out completed);
return new string(chars);
}
示例2: ValidateSerializeDeserialize
partial static void ValidateSerializeDeserialize(Encoding e)
{
// Make sure the Encoding roundtrips
Assert.Equal(e, BinaryFormatterHelpers.Clone(e));
// Get an encoder and decoder from the encoding, and clone them
Encoder origEncoder = e.GetEncoder();
Decoder origDecoder = e.GetDecoder();
Encoder clonedEncoder = BinaryFormatterHelpers.Clone(origEncoder);
Decoder clonedDecoder = BinaryFormatterHelpers.Clone(origDecoder);
// Encode and decode some text with each pairing
const string InputText = "abcdefghijklmnopqrstuvwxyz";
char[] inputTextChars = InputText.ToCharArray();
var pairs = new[]
{
Tuple.Create(origEncoder, origDecoder),
Tuple.Create(origEncoder, clonedDecoder),
Tuple.Create(clonedEncoder, origDecoder),
Tuple.Create(clonedEncoder, clonedDecoder),
};
var results = new List<char[]>();
foreach (Tuple<Encoder, Decoder> pair in pairs)
{
byte[] encodedBytes = new byte[pair.Item1.GetByteCount(inputTextChars, 0, inputTextChars.Length, true)];
Assert.Equal(encodedBytes.Length, pair.Item1.GetBytes(inputTextChars, 0, inputTextChars.Length, encodedBytes, 0, true));
char[] decodedChars = new char[pair.Item2.GetCharCount(encodedBytes, 0, encodedBytes.Length)];
Assert.Equal(decodedChars.Length, pair.Item2.GetChars(encodedBytes, 0, encodedBytes.Length, decodedChars, 0));
results.Add(decodedChars);
}
// Validate that all of the pairings produced the same results
foreach (char[] a in results)
{
foreach (char[] b in results)
{
Assert.Equal(a, b);
}
}
}
示例3: DecodeByteArryToString
/// <summary>
/// Converts byte array to string, using decoding as requested
/// </summary>
public string DecodeByteArryToString(byte[] ByteArry, Encoding ByteEncoding)
{
if (ByteArry == null)
{
//no bytes to convert
return null;
}
Decoder byteArryDecoder;
if (ByteEncoding == null)
{
//no encoding indicated. Let's try UTF7
System.Diagnostics.Debugger.Break(); //didn't have a sample email to test this
byteArryDecoder = Encoding.UTF7.GetDecoder();
}
else
{
byteArryDecoder = ByteEncoding.GetDecoder();
}
int charCount = byteArryDecoder.GetCharCount(ByteArry, 0, ByteArry.Length);
char[] bodyChars = new char[charCount - 1+ 1];
int charsDecodedCount = byteArryDecoder.GetChars(ByteArry, 0, ByteArry.Length, bodyChars, 0);
//convert char[] to string
return new string(bodyChars);
}
示例4: HtmlStream
public HtmlStream(Stream stm, Encoding defaultEncoding) {
if(defaultEncoding == null) {
defaultEncoding = Encoding.UTF8; // default is UTF8
}
if(!stm.CanSeek) {
// Need to be able to seek to sniff correctly.
stm = CopyToMemoryStream(stm);
}
this.stm = stm;
rawBuffer = new Byte[BUFSIZE];
rawUsed = stm.Read(rawBuffer, 0, 4); // maximum byte order mark
this.m_buffer = new char[BUFSIZE];
// Check byte order marks
this.m_decoder = AutoDetectEncoding(rawBuffer, ref rawPos, rawUsed);
var bom = rawPos;
if(this.m_decoder == null) {
this.m_decoder = defaultEncoding.GetDecoder();
rawUsed += stm.Read(rawBuffer, 4, BUFSIZE - 4);
DecodeBlock();
// Now sniff to see if there is an XML declaration or HTML <META> tag.
var sd = SniffEncoding();
if(sd != null) {
this.m_decoder = sd;
}
}
// Reset to get ready for Read()
this.stm.Seek(0, SeekOrigin.Begin);
this.pos = this.used = 0;
// skip bom
if(bom > 0) {
stm.Read(this.rawBuffer, 0, bom);
}
this.rawPos = this.rawUsed = 0;
}
示例5: DecoderTest
public DecoderTest (string cnvout, Encoding encoding) : base (cnvout)
{
dec = encoding.GetDecoder ();
}
示例6: EndianBinaryReader
/// <summary>
/// Constructs a new binary reader with the given bit converter, reading
/// to the given stream, using the given encoding.
/// </summary>
/// <param name="bitConverter">Converter to use when reading data</param>
/// <param name="stream">Stream to read data from</param>
/// <param name="encoding">Encoding to use when reading character data</param>
public EndianBinaryReader(EndianBitConverter bitConverter, Stream stream, Encoding encoding)
{
if (bitConverter == null)
{
throw new ArgumentNullException("bitConverter");
}
if (stream == null)
{
throw new ArgumentNullException("stream");
}
if (encoding == null)
{
throw new ArgumentNullException("encoding");
}
if (!stream.CanRead)
{
throw new ArgumentException("Stream isn't writable", "stream");
}
this.stream = stream;
this.bitConverter = bitConverter;
this.encoding = encoding;
this.decoder = encoding.GetDecoder();
this.minBytesPerChar = 1;
if (encoding is UnicodeEncoding)
{
minBytesPerChar = 2;
}
}