本文整理汇总了C#中Encoding类的典型用法代码示例。如果您正苦于以下问题:C# Encoding类的具体用法?C# Encoding怎么用?C# Encoding使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Encoding类属于命名空间,在下文中一共展示了Encoding类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetBytes
public static void GetBytes(Encoding encoding, string source, int index, int count, byte[] bytes, int byteIndex, byte[] expectedBytes)
{
byte[] originalBytes = (byte[])bytes.Clone();
if (index == 0 && count == source.Length)
{
// Use GetBytes(string)
byte[] stringResultBasic = encoding.GetBytes(source);
VerifyGetBytes(stringResultBasic, 0, stringResultBasic.Length, originalBytes, expectedBytes);
// Use GetBytes(char[])
byte[] charArrayResultBasic = encoding.GetBytes(source.ToCharArray());
VerifyGetBytes(charArrayResultBasic, 0, charArrayResultBasic.Length, originalBytes, expectedBytes);
}
// Use GetBytes(char[], int, int)
byte[] charArrayResultAdvanced = encoding.GetBytes(source.ToCharArray(), index, count);
VerifyGetBytes(charArrayResultAdvanced, 0, charArrayResultAdvanced.Length, originalBytes, expectedBytes);
// Use GetBytes(string, int, int, byte[], int)
byte[] stringBytes = (byte[])bytes.Clone();
int stringByteCount = encoding.GetBytes(source, index, count, stringBytes, byteIndex);
VerifyGetBytes(stringBytes, byteIndex, stringByteCount, originalBytes, expectedBytes);
Assert.Equal(expectedBytes.Length, stringByteCount);
// Use GetBytes(char[], int, int, byte[], int)
byte[] charArrayBytes = (byte[])bytes.Clone();
int charArrayByteCount = encoding.GetBytes(source.ToCharArray(), index, count, charArrayBytes, byteIndex);
VerifyGetBytes(charArrayBytes, byteIndex, charArrayByteCount, originalBytes, expectedBytes);
Assert.Equal(expectedBytes.Length, charArrayByteCount);
}
示例2: CreateStream
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding,
string mimeType, bool willSeek)
{
Stream stream = new FileStream(name + "." + fileNameExtension, FileMode.Create);
m_streams.Add(stream);
return stream;
}
示例3: GetByteCount_Invalid
public static unsafe void GetByteCount_Invalid(Encoding encoding)
{
// Chars is null
Assert.Throws<ArgumentNullException>(encoding is ASCIIEncoding ? "chars" : "s", () => encoding.GetByteCount((string)null));
Assert.Throws<ArgumentNullException>("chars", () => encoding.GetByteCount((char[])null));
Assert.Throws<ArgumentNullException>("chars", () => encoding.GetByteCount(null, 0, 0));
// Index or count < 0
Assert.Throws<ArgumentOutOfRangeException>("index", () => encoding.GetByteCount(new char[3], -1, 0));
Assert.Throws<ArgumentOutOfRangeException>("count", () => encoding.GetByteCount(new char[3], 0, -1));
// Index + count > chars.Length
Assert.Throws<ArgumentOutOfRangeException>("chars", () => encoding.GetByteCount(new char[3], 0, 4));
Assert.Throws<ArgumentOutOfRangeException>("chars", () => encoding.GetByteCount(new char[3], 1, 3));
Assert.Throws<ArgumentOutOfRangeException>("chars", () => encoding.GetByteCount(new char[3], 2, 2));
Assert.Throws<ArgumentOutOfRangeException>("chars", () => encoding.GetByteCount(new char[3], 3, 1));
Assert.Throws<ArgumentOutOfRangeException>("chars", () => encoding.GetByteCount(new char[3], 4, 0));
char[] chars = new char[3];
fixed (char* pChars = chars)
{
char* pCharsLocal = pChars;
Assert.Throws<ArgumentNullException>("chars", () => encoding.GetByteCount(null, 0));
Assert.Throws<ArgumentOutOfRangeException>("count", () => encoding.GetByteCount(pCharsLocal, -1));
}
}
示例4: CsvStreamReader
/// <summary>
///
/// </summary>
/// <param name="fileName">文件名,包括文件路径</param>
/// <param name="encoding">文件编码</param>
public CsvStreamReader(string fileName, Encoding encoding)
{
this.rowAL = new ArrayList();
this.fileName = fileName;
this.encoding = encoding;
LoadCsvFile();
}
示例5: ExchangeHTTP
public ExchangeHTTP(HttpResponse response, string flag)
{
this.encoding = response.Output.Encoding;
this.responseStream = response.Filter;
this.flag = flag;
}
示例6: VerifyEncoding
public static void VerifyEncoding(Encoding encoding, int codePage, EncoderFallback encoderFallback, DecoderFallback decoderFallback)
{
if (encoderFallback == null)
{
Assert.NotNull(encoding.EncoderFallback);
Assert.Equal(codePage, encoding.EncoderFallback.GetHashCode());
}
else
{
Assert.Same(encoderFallback, encoding.EncoderFallback);
}
if (decoderFallback == null)
{
Assert.NotNull(encoding.DecoderFallback);
Assert.Equal(codePage, encoding.DecoderFallback.GetHashCode());
}
else
{
Assert.Same(decoderFallback, encoding.DecoderFallback);
}
Assert.Empty(encoding.GetPreamble());
Assert.False(encoding.IsSingleByte);
}
示例7: Decode
public override bool Decode(Encoding encoding)
{
// List<object> encoded = new List<object>(encodings);
if (!encoding.Validate( EncodingType.Group,
EncodingType.Group
))
{
Debug.Log ("Could not decode generator \n"+encoding.DebugString());
return false;
}
Debug.Log ("Decoding Generator: "+encoding.DebugString());
base.Decode(encoding.SubEncoding(0));
if (toGenerateConstruction != null)
{
ObjectPoolManager.DestroyObject(toGenerateConstruction);
toGenerateConstruction = null;
}
if (encoding.Count > 1)
{
toGenerateConstruction = Construction.DecodeCreate(encoding.SubEncoding(1));
toGenerateConstruction.ignoreCollisions = true;
}
return true;
}
示例8: ReadToEnd
/// <summary>
/// A Stream extension method that reads a stream to the end.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="encoding">The encoding.</param>
/// <returns>
/// The rest of the stream as a string, from the current position to the end. If the current position is at the
/// end of the stream, returns an empty string ("").
/// </returns>
public static string ReadToEnd(this Stream @this, Encoding encoding)
{
using (var sr = new StreamReader(@this, encoding))
{
return sr.ReadToEnd();
}
}
示例9: Decode
public virtual bool Decode (Encoding encoding)
{
IntVector2 newLocation = new IntVector2(encoding.Int(0)-31, encoding.Int (1)-31);
Mechanism placedMechanism = GridManager.instance.GetHexCell(newLocation).placedMechanism;
bool didReplacedPart = false;
if (placedMechanism != null) // we are replacing a part
{
if (placedMechanism.MechanismType == MechanismType && // we are replacing the same type of part
placedMechanism.Location.IsEqualTo(newLocation) && // the location of the old part is the same as this new one (important for multicell mechanisms e.g. weldingRig)
!placedMechanism.isSolutionMechanism) // is a level mechanism (not part of the solution, part of the problem ;p)
{
ObjectPoolManager.DestroyObject(placedMechanism);
PlaceAtLocation(newLocation);
isSolutionMechanism = false; // we use the already on board's movable (i.e. immovable)
}
else
{
// something went wrong, we are loading a mechanism on top of one that is different, or a solution mechanism
Debug.LogError("Something went wrong, we are loading a mechanism on top of one that is different, or a solution mechanism");
return false;
}
}
else // this is a new part
{
PlaceAtLocation(newLocation);
isSolutionMechanism = (int)encoding.Int(2) == 1;
}
return true;
}
示例10: GetHttpRequestStringByNUll_Get
///<summary>
///采用https协议GET方式访问网络,根据传入的URl地址,得到响应的数据字符串。
///</summary>
///<param name="URL">url地址</param>
///<param name="objencoding">编码方式例如:System.Text.Encoding.UTF8;</param>
///<returns>String类型的数据</returns>
public string GetHttpRequestStringByNUll_Get(string URL, Encoding objencoding)
{
//准备参数
SetRequest(URL, "GET", "text/html, application/xhtml+xml, */*", "text/html", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)", objencoding);
//调用专门读取数据的类
return GetHttpRequestData("");
}
示例11: Stringify
public static string Stringify(this Stream theStream, Encoding encoding = null)
{
using (var reader = new StreamReader(theStream, encoding ?? DefaultEncoding))
{
return reader.ReadToEnd();
}
}
示例12: GetHttpRequestString
/// <summary>
/// 采用https协议GET|POST方式访问网络,根据传入的URl地址,得到响应的数据字符串。
/// </summary>
/// <param name="_URL"></param>
/// <param name="_Method">请求方式Get或者Post</param>
/// <param name="_Accept">Accept</param>
/// <param name="_ContentType">ContentType返回类型</param>
/// <param name="_UserAgent">UserAgent客户端的访问类型,包括浏览器版本和操作系统信息</param>
/// <param name="_Encoding">读取数据时的编码方式</param>
/// <param name="_Postdata">只有_Method为Post方式时才需要传入值</param>
/// <returns>返回Html源代码</returns>
public string GetHttpRequestString(string _URL, string _Method, string _Accept, string _ContentType, string _UserAgent, Encoding _Encoding, string _Postdata)
{
//准备参数
SetRequest(_URL, _Method, _Accept, _ContentType, _UserAgent, _Encoding);
//调用专门读取数据的类
return GetHttpRequestData(_Postdata);
}
示例13: 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;
}
}
示例14: Convert
public void Convert(Encoding srcEncoding, Encoding dstEncoding, byte[] bytes, int index, int count, byte[] expected)
{
if (index == 0 && count == bytes.Length)
{
Assert.Equal(expected, Encoding.Convert(srcEncoding, dstEncoding, bytes));
}
Assert.Equal(expected, Encoding.Convert(srcEncoding, dstEncoding, bytes, index, count));
}
示例15: CSVReader
/// <summary>
/// Create a new reader for the given stream and encoding.
/// </summary>
/// <param name="s">The stream to read the CSV from.</param>
/// <param name="enc">The encoding used.</param>
public CSVReader(Stream s, Encoding enc)
{
this.stream = s;
if (!s.CanRead)
{
throw new CSVReaderException("Could not read the given CSV stream!");
}
reader = (enc != null) ? new StreamReader(s, enc) : new StreamReader(s);
}