本文整理汇总了C#中Encoding类的典型用法代码示例。如果您正苦于以下问题:C# Encoding类的具体用法?C# Encoding怎么用?C# Encoding使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Encoding类属于命名空间,在下文中一共展示了Encoding类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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);
}
开发者ID:Dmitry-Me,项目名称:corefx,代码行数:30,代码来源:EncodingTestHelpers.cs
示例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;
}
开发者ID:iMutex,项目名称:EBusiness,代码行数:7,代码来源:AutoPrintingDemo.cs
示例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));
}
}
开发者ID:SGuyGe,项目名称:corefx,代码行数:26,代码来源:NegativeEncodingTests.cs
示例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();
}
开发者ID:chanfengsr,项目名称:AllPrivateProject,代码行数:12,代码来源:CsvStreamReader.cs
示例5: ExchangeHTTP
public ExchangeHTTP(HttpResponse response, string flag)
{
this.encoding = response.Output.Encoding;
this.responseStream = response.Filter;
this.flag = flag;
}
开发者ID:foxvirtuous,项目名称:MVB2C,代码行数:7,代码来源:ScriptDeferFilter.cs
示例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);
}
开发者ID:ESgarbi,项目名称:corefx,代码行数:25,代码来源:EncodingCtorTests.cs
示例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;
}
开发者ID:raxter,项目名称:6-Fold-Mass-Production,代码行数:27,代码来源:PartGenerator.cs
示例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();
}
}
开发者ID:fqybzhangji,项目名称:Z.ExtensionMethods,代码行数:16,代码来源:Stream.ReadToEnd.cs
示例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;
}
开发者ID:raxter,项目名称:6-Fold-Mass-Production,代码行数:32,代码来源:Mechanism.cs
示例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("");
}
开发者ID:tianmaoyu,项目名称:TaoBaoGun,代码行数:13,代码来源:HttpHelps.cs
示例11: Stringify
public static string Stringify(this Stream theStream, Encoding encoding = null)
{
using (var reader = new StreamReader(theStream, encoding ?? DefaultEncoding))
{
return reader.ReadToEnd();
}
}
开发者ID:CertiPay,项目名称:CertiPay.Common.Encryption,代码行数:7,代码来源:PGPUtilities.cs
示例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);
}
开发者ID:tianmaoyu,项目名称:TaoBaoGun,代码行数:18,代码来源:HttpHelps.cs
示例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;
}
}
开发者ID:RaviChimmalgi,项目名称:fishbowl-shopify,代码行数:36,代码来源:BigEndianReader.cs
示例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));
}
开发者ID:ChuangYang,项目名称:corefx,代码行数:8,代码来源:EncodingConvertTests.cs
示例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);
}
开发者ID:tradeshark,项目名称:TradeShark,代码行数:14,代码来源:CVSReader.cs
示例16: Json
protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
{
return new JsonNetResult
{
Data = data,
ContentType = contentType,
ContentEncoding = contentEncoding,
JsonRequestBehavior = behavior
};
}
开发者ID:yuzhiping,项目名称:HyCtbuEco,代码行数:10,代码来源:BaseController.cs
示例17: GetCharCount
public static void GetCharCount(Encoding encoding, byte[] bytes, int index, int count, int expected)
{
if (index == 0 && count == bytes.Length)
{
// Use GetCharCount(byte[])
Assert.Equal(expected, encoding.GetCharCount(bytes));
}
// Use GetCharCount(byte[], int, int)
Assert.Equal(expected, encoding.GetCharCount(bytes, index, count));
}
开发者ID:Dmitry-Me,项目名称:corefx,代码行数:10,代码来源:EncodingTestHelpers.cs
示例18: TestEncoding
private void TestEncoding(Encoding enc, int byteCount, int maxByteCount, byte[] bytes)
{
Assert.Equal(byteCount, enc.GetByteCount(s_myChars));
Assert.Equal(maxByteCount, enc.GetMaxByteCount(s_myChars.Length));
Assert.Equal(enc.GetBytes(s_myChars), bytes);
Assert.Equal(enc.GetCharCount(bytes), s_myChars.Length);
Assert.Equal(enc.GetChars(bytes), s_myChars);
Assert.Equal(enc.GetString(bytes, 0, bytes.Length), s_myString);
Assert.NotEqual(0, enc.GetHashCode());
}
开发者ID:noahfalk,项目名称:corefx,代码行数:10,代码来源:ConvertUnicodeEncodings.cs
示例19: ReadToEnd
/// <summary>
/// A FileInfo extension method that reads the file 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 FileInfo @this, Encoding encoding)
{
using (FileStream stream = File.Open(@this.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (var reader = new StreamReader(stream, encoding))
{
return reader.ReadToEnd();
}
}
}
开发者ID:fqybzhangji,项目名称:Z.ExtensionMethods,代码行数:19,代码来源:FileInfo.ReadToEnd.cs
示例20: SaveAs
/// <summary>
/// A string extension method that save the string into a file.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="fileName">Filename of the file.</param>
/// <param name="append">(Optional) if the text should be appended to file file if it's exists.</param>
/// <example>
/// <code>
/// using System;
/// using System.IO;
/// using Microsoft.VisualStudio.TestTools.UnitTesting;
/// using Z.ExtensionMethods;
///
/// namespace ExtensionMethods.Examples
/// {
/// [TestClass]
/// public class System_String_SaveAs
/// {
/// [TestMethod]
/// public void SaveAs()
/// {
/// var fileInfo = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Examples_System_String_SaveAs.txt"));
/// string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Examples_System_String_SaveAs2.txt");
///
/// // Type
/// string @this = "Fizz";
///
/// // Examples
/// @this.SaveAs(filePath); // Save string in a file.
/// @this.SaveAs(fileInfo); // Save string in a file.
/// @this.SaveAs(fileInfo, true); // Append string to existing file.
///
/// // Unit Test
/// Assert.IsTrue(fileInfo.Exists);
/// Assert.IsTrue(new FileInfo(filePath).Exists);
/// }
/// }
/// }
/// </code>
/// </example>
public static void SaveAs(this string @this, string fileName, bool append = false, Encoding enc = null)
{
var toadd = "";
if (File.Exists(fileName)) {
if (append)
toadd = File.ReadAllText(fileName);
File.SetAttributes(fileName, FileAttributes.Normal);
File.Delete(fileName);
}
File.WriteAllText(fileName, toadd + @this, enc??Encoding.UTF8);
}
开发者ID:Nucs,项目名称:nlib,代码行数:51,代码来源:String.SaveAs.cs
注:本文中的Encoding类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论