本文整理汇总了C#中Encoding.GetString方法的典型用法代码示例。如果您正苦于以下问题:C# Encoding.GetString方法的具体用法?C# Encoding.GetString怎么用?C# Encoding.GetString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Encoding
的用法示例。
在下文中一共展示了Encoding.GetString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetString
public static void GetString(Encoding encoding, byte[] bytes, int index, int count, string expected)
{
if (index == 0 && count == bytes.Length)
{
// Use GetString(byte[])
Assert.Equal(expected, encoding.GetString(bytes));
}
// Use GetString(byte[], int, int)
Assert.Equal(expected, encoding.GetString(bytes, index, count));
}
示例2: 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());
}
示例3: ReadStringAsync
public static async Task<string> ReadStringAsync(this IUVStream stream, Encoding encoding)
{
if (encoding == null) {
throw new ArgumentException("encoding");
}
var buffer = await stream.ReadAsync();
if (buffer == null) {
return null;
}
return encoding.GetString(buffer.Buffer, buffer.Start, buffer.Length);
}
示例4: ReadStringAsync
public static async Task<string> ReadStringAsync(this IUVStream<ArraySegment<byte>> stream, Encoding encoding)
{
if (encoding == null) {
throw new ArgumentException("encoding");
}
var buffer = await stream.ReadStructAsync();
if (!buffer.HasValue) {
return null;
}
return encoding.GetString(buffer.Value);
}
示例5: ReadStringAsync
public static async Task<string> ReadStringAsync(this IUVStream stream, Encoding encoding)
{
if (encoding == null) {
throw new ArgumentException("encoding");
}
var buffer = await stream.ReadAsync();
if (!buffer.HasValue) {
return null;
}
var b = buffer.Value;
return encoding.GetString(b.Array, b.Offset, b.Count);
}
示例6: ToStringFull
public static String ToStringFull(this XDocument That, Encoding Encoding)
{
var VersionXmlStream = new MemoryStream();
var VersionXmlWriter = XmlWriter.Create(VersionXmlStream, new XmlWriterSettings()
{
Encoding = Encoding,
Indent = true,
});
That.WriteTo(VersionXmlWriter);
VersionXmlWriter.Flush();
return Encoding.GetString(VersionXmlStream.ToArray());
}
示例7: read
public static string read(Stream s, long count, Encoding enc)
{
string str = "";
while (count > 0)
{
byte[] buffer = new byte[1];
try
{
s.Read(buffer, 0, 1);
}
catch
{
return null;
}
str += enc.GetString(buffer);
count--;
}
return str;
}
示例8: Load
public int Load(string path, Encoding encode)
{
try
{
FileStream fs = new FileStream(Application.persistentDataPath + "/" + path, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
var bin = new System.Collections.Generic.List<byte>();
try
{
while (true) { bin.Add(br.ReadByte()); }
}
catch (EndOfStreamException) { }
str = encode.GetString(aes.Decrypt(bin.ToArray()));
br.Close();
}
catch {
// ロード失敗
return -1;
}
return 0;
}
示例9: DecompressGZip
/// <summary>
/// A byte[] extension method that decompress the byte array gzip to string.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="encoding">The encoding.</param>
/// <returns>The byte array gzip to string.</returns>
public static string DecompressGZip(this byte[] @this, Encoding encoding)
{
const int bufferSize = 1024;
using (var memoryStream = new MemoryStream(@this))
{
using (var zipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
{
// Memory stream for storing the decompressed bytes
using (var outStream = new MemoryStream())
{
var buffer = new byte[bufferSize];
int totalBytes = 0;
int readBytes;
while ((readBytes = zipStream.Read(buffer, 0, bufferSize)) > 0)
{
outStream.Write(buffer, 0, readBytes);
totalBytes += readBytes;
}
return encoding.GetString(outStream.GetBuffer(), 0, totalBytes);
}
}
}
}
示例10: ValidateConsoleEncoding
public static unsafe void ValidateConsoleEncoding(Encoding encoding)
{
Assert.NotNull(encoding);
// The primary purpose of ConsoleEncoding is to return an empty preamble.
Assert.Equal(Array.Empty<byte>(), encoding.GetPreamble());
// There's not much validation we can do, but we can at least invoke members
// to ensure they don't throw exceptions as they delegate to the underlying
// encoding wrapped by ConsoleEncoding.
Assert.False(string.IsNullOrWhiteSpace(encoding.EncodingName));
Assert.False(string.IsNullOrWhiteSpace(encoding.WebName));
Assert.True(encoding.CodePage >= 0);
bool ignored = encoding.IsSingleByte;
// And we can validate that the encoding is self-consistent by roundtripping
// data between chars and bytes.
string str = "This is the input string.";
char[] strAsChars = str.ToCharArray();
byte[] strAsBytes = encoding.GetBytes(str);
Assert.Equal(strAsBytes.Length, encoding.GetByteCount(str));
Assert.True(encoding.GetMaxByteCount(str.Length) >= strAsBytes.Length);
Assert.Equal(str, encoding.GetString(strAsBytes));
Assert.Equal(str, encoding.GetString(strAsBytes, 0, strAsBytes.Length));
Assert.Equal(str, new string(encoding.GetChars(strAsBytes)));
Assert.Equal(str, new string(encoding.GetChars(strAsBytes, 0, strAsBytes.Length)));
fixed (byte* bytesPtr = strAsBytes)
{
char[] outputArr = new char[encoding.GetMaxCharCount(strAsBytes.Length)];
int len = encoding.GetChars(strAsBytes, 0, strAsBytes.Length, outputArr, 0);
Assert.Equal(str, new string(outputArr, 0, len));
Assert.Equal(len, encoding.GetCharCount(strAsBytes));
Assert.Equal(len, encoding.GetCharCount(strAsBytes, 0, strAsBytes.Length));
fixed (char* charsPtr = outputArr)
{
len = encoding.GetChars(bytesPtr, strAsBytes.Length, charsPtr, outputArr.Length);
Assert.Equal(str, new string(charsPtr, 0, len));
Assert.Equal(len, encoding.GetCharCount(bytesPtr, strAsBytes.Length));
}
Assert.Equal(str, encoding.GetString(bytesPtr, strAsBytes.Length));
}
Assert.Equal(strAsBytes, encoding.GetBytes(strAsChars));
Assert.Equal(strAsBytes, encoding.GetBytes(strAsChars, 0, strAsChars.Length));
Assert.Equal(strAsBytes.Length, encoding.GetByteCount(strAsChars));
Assert.Equal(strAsBytes.Length, encoding.GetByteCount(strAsChars, 0, strAsChars.Length));
fixed (char* charsPtr = strAsChars)
{
Assert.Equal(strAsBytes.Length, encoding.GetByteCount(charsPtr, strAsChars.Length));
byte[] outputArr = new byte[encoding.GetMaxByteCount(strAsChars.Length)];
Assert.Equal(strAsBytes.Length, encoding.GetBytes(strAsChars, 0, strAsChars.Length, outputArr, 0));
fixed (byte* bytesPtr = outputArr)
{
Assert.Equal(strAsBytes.Length, encoding.GetBytes(charsPtr, strAsChars.Length, bytesPtr, outputArr.Length));
}
Assert.Equal(strAsBytes.Length, encoding.GetBytes(str, 0, str.Length, outputArr, 0));
}
}
示例11: af_ReadAllText
public static String af_ReadAllText(String path, Encoding encoding)
{
return encoding.GetString(af_ReadAllBytes(path));
}
示例12: UrlDecode
/// <summary>
///
/// </summary>
/// <param name="s">�f�R�[�h���镶����</param>
/// <param name="enc">�G���R�[�f�B���O</param>
/// <returns></returns>
public static string UrlDecode(string s, Encoding enc)
{
List<byte> bytes = new List<byte>();
for (int i = 0; i < s.Length; i++)
{
char c = s[i];
if (c == '%')
bytes.Add(byte.Parse(s[++i].ToString() + s[++i].ToString()));
else if (c == '+')
bytes.Add((byte)0x20);
else
bytes.Add((byte)c);
}
return enc.GetString(bytes.ToArray(), 0, bytes.Count);
}
示例13: ByteArrayToString
public static string ByteArrayToString(byte[] ByteArray, Encoding encoding)
{
return encoding.GetString(ByteArray);
}
示例14: Dump
/// <summary>
/// Prints all pending messages to the console.
/// </summary>
/// <param name="socket">ZMQ Socket</param>
/// <param name="encoding">Encoding to use for message decoding</param>
public static void Dump(Socket socket, Encoding encoding) {
if (socket == null) {
throw new ArgumentNullException("socket");
}
Console.WriteLine(new String('-', 38));
foreach (byte[] msg in socket.RecvAll()) {
Console.Write("[{0}] ", String.Format("{0:d3}", msg.Length));
if (msg.Length == 17 && msg[0] == 0) {
Console.WriteLine(DecodeUUID(msg).Substring(1));
}
else {
Console.WriteLine(encoding.GetString(msg, 0, msg.Length));
}
}
}
示例15: GetDynamicJsonObject
public static dynamic GetDynamicJsonObject(byte[] buf, Encoding encoding)
{
return GetDynamicJsonObject(encoding.GetString(buf));
}