本文整理汇总了C#中System.Text.Encoding.GetChars方法的典型用法代码示例。如果您正苦于以下问题:C# Encoding.GetChars方法的具体用法?C# Encoding.GetChars怎么用?C# Encoding.GetChars使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Text.Encoding
的用法示例。
在下文中一共展示了Encoding.GetChars方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DataToChars
private char[]/*!*/ DataToChars(int additionalCapacity, Encoding/*!*/ encoding) {
if (_count == 0) {
return (additionalCapacity == 0) ? Utils.EmptyChars : new char[additionalCapacity];
} else if (additionalCapacity == 0) {
return encoding.GetChars(_data, 0, _count);
} else {
var result = new char[encoding.GetCharCount(_data, 0, _count) + additionalCapacity];
encoding.GetChars(_data, 0, _count, result, 0);
return result;
}
}
示例2: ToTrimmedString
public static string ToTrimmedString(byte[] buffer, Encoding encoding)
{
var encoded = encoding.GetChars(buffer);
var i = 0;
while (i < encoded.Length && encoded[i] != 0) i++;
return encoding.GetString(encoding.GetBytes(encoded.Take(i).ToArray()));
}
示例3: Create
public static unsafe FFXIIICodePage Create(Encoding encoding)
{
byte[] buff = new byte[256];
char[] chars = new char[256 + 0x2C10];
fixed (byte* buffPtr = &buff[0])
fixed (char* charsPtr = &chars[0])
{
for (int b = 0; b < 256; b++)
buffPtr[b] = (byte)b;
encoding.GetChars(buffPtr, buff.Length, charsPtr, buff.Length);
}
CreateAdditionalCharacters(chars);
Dictionary<char, short> bytes = new Dictionary<char, short>(chars.Length);
for (int i = chars.Length - 1; i >= 0; i--)
{
char ch = chars[i];
switch (ch)
{
case '\0':
continue;
}
bytes[chars[i]] = (short)i;
}
return new FFXIIICodePage(chars, bytes);
}
示例4: CompressToChar
public static char[] CompressToChar(char[] str, bool needheadflag, Encoding enc)
{
if (str.Length < zipsizemin) return str;
Byte[] bTytes = enc.GetBytes(str);
Byte[] retbytes = Compress(bTytes, needheadflag, enc);
return enc.GetChars(retbytes);
}
示例5: 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());
}
示例6: ReadString
public string ReadString(int length, Encoding enc)
{
byte[] buf=new byte[length];
int amountRead=Read(buf, 0, length);
if ( amountRead != length )
throw new InvalidOperationException("End of file while reading a string");
char[] chars=enc.GetChars(buf);
return new string(chars);
}
示例7: Decode
//Перекодирует value из кодировки encoder в кодировку decoder
public string Decode(Encoding encoder, Encoding decoder, string value)
{
byte[] intByteBuff, charBuff, outByteBuff, codeBytes;
char[] chars;
codeBytes = new byte[encoder.GetByteCount(value)];
encoder.GetBytes(value, 0, value.Length, codeBytes, 0);
chars = decoder.GetChars(codeBytes);
return new string(chars);
}
示例8: ToEncoding
public static String ToEncoding(this String psValue, Encoding poSourceEncoding, Encoding poDestinationEncoding)
{
if (String.IsNullOrEmpty(psValue))
{
return psValue;
}
var loSourceBytes = poSourceEncoding.GetBytes(psValue);
var loDestinationBytes = Encoding.Convert(poSourceEncoding, poDestinationEncoding, loSourceBytes);
var loDestinationChars = new char[poDestinationEncoding.GetCharCount(loDestinationBytes, 0, loDestinationBytes.Length)];
poDestinationEncoding.GetChars(loDestinationBytes, 0, loDestinationChars.Length, loDestinationChars, 0);
return new string(loDestinationChars);
}
示例9: ConvertEncoding
private static void ConvertEncoding(Encoding unicode, Encoding ascii)
{
string storyline = "Cobb's rare ability";
var sourceBytes = ascii.GetBytes(storyline);
var destBytes = Encoding.Convert(ascii, unicode, sourceBytes);
var destChars = new char[unicode.GetCharCount(destBytes, 0, destBytes.Length)];
unicode.GetChars(destBytes, 0, destBytes.Length, destChars, 0);
var newString = new string(destChars);
Console.WriteLine("\n-------------------------------");
Console.WriteLine(DateTime.Now);
Console.WriteLine(unicode.EncodingName);
Console.WriteLine(ascii.EncodingName);
Console.WriteLine("===============================");
Console.WriteLine(newString);
}
示例10: Convert
// Convert between two encodings.
public static byte[] Convert(Encoding srcEncoding, Encoding dstEncoding,
byte[] bytes)
{
if(srcEncoding == null)
{
throw new ArgumentNullException("srcEncoding");
}
if(dstEncoding == null)
{
throw new ArgumentNullException("dstEncoding");
}
if(bytes == null)
{
throw new ArgumentNullException("bytes");
}
return dstEncoding.GetBytes(srcEncoding.GetChars
(bytes, 0, bytes.Length));
}
示例11: Compare
public static void Compare(Encoding encoding, Encoding winEncoding, int range)
{
StringBuilder encodingChars = new StringBuilder();
StringBuilder winChars = new StringBuilder();
char[] encodingCharArray = new char[range];
char[] winCharArray = new char[range];
//decode bytes to characters / string
for (int i = 0; i < range; i++)
{
char encodingChar = encoding.GetChars(new[] {(byte) i}).Single();
char winChar = winEncoding.GetChars(new[] {(byte) i}).Single();
Debug.Assert(encodingChar == winChar, "Got different characters for byte " + i + ": " + encodingChar + " / " + winChar);
encodingCharArray[i] = encodingChar;
winCharArray[i] = winChar;
encodingChars.Append(encodingChar);
winChars.Append(winChar);
}
//encode characters
byte[] encodingBytes = encoding.GetBytes(encodingCharArray);
byte[] winBytes = encoding.GetBytes(winCharArray);
Debug.Assert(encodingBytes.Length == winBytes.Length,
"Encoded char arrays return byte arrays of different sizes: " + encodingBytes.Length + " vs. " +
winBytes.Length);
for (int i = 0; i < encodingBytes.Length; i++)
{
byte encodingByte = encodingBytes[i];
byte winByte = winBytes[i];
Debug.Assert(encodingByte == winByte, "Got different bytes at index " + i + ": " + encodingByte + " / " + winByte);
}
Console.Out.WriteLine("Compared encodings successfully for " + range + " bytes.");
}
示例12: ReadLine
/// <summary>
/// Reads a single line from the Stream
/// </summary>
/// <param name="stream">The stream to read from</param>
/// <param name="encoding">The encoding to interpret the text</param>
/// <returns>The line read, without the newline character(s)</returns>
public static string ReadLine(this Stream stream, Encoding encoding)
{
byte[] buffer = new byte[8]; // max character bytes
int bufferPos = 0;
StringBuilder line = new StringBuilder(100);
int c;
bool lineRead = false;
Decoder decoder = encoding.GetDecoder();
while ((c = stream.ReadByte()) != -1)
{
if (c == '\n')
{
lineRead = true;
// strip any '\r' immediately preceding
if ((line.Length > 0) && (line[line.Length - 1] == '\r'))
{
line.Remove(line.Length - 1, 1);
}
// and break
break;
}
if (bufferPos >= buffer.Length)
throw new InvalidDataException("Invalid character read from stream.");
buffer[bufferPos++] = (byte)c;
if (decoder.GetCharCount(buffer, 0, bufferPos) != 1) continue;
line.Append(encoding.GetChars(buffer, 0, bufferPos));
bufferPos = 0;
}
if (c == -1 && lineRead == false)
throw new IOException("Stream shut down while reading line.");
return line.ToString();
}
示例13: ReadVariableString
/// <summary>
/// Reads the variable-length string that is terminated either by zero-char or end of stream.
/// After reading, the position will be set after zero-char terminator.
/// </summary>
/// <param name="encoding">The encoding.</param>
/// <returns>String</returns>
public string ReadVariableString(Encoding encoding)
{
// This method reads a block of bytes to "bytes" from stream, then it decodes it into "chars" by using
// the decoder of specified "encoding". This ensures correct decoding of single- or multi-byte strings.
// Then "chars" are scanned for zero-char (terminator) or end of stream. Non-zero chars are added to the
// output "text" list and then returned as string.
// If zero-char is found then the stream postion is returned back to first byte after zero-char.
// This seems to be better than using some complex zero-char locator method and then calling
// Encoding.GetString() method.
var text = new List<char>(_SIZE);
var bytes = GetBytesBuffer();
var chars = GetCharsBuffer();
var decoder = encoding.GetDecoder();
var encoder = encoding.GetEncoder();
var isTerminated = false;
decoder.Reset();
while (!isTerminated)
{
var byteCount = _stream.Read(bytes, 0, _SIZE);
var charCount = encoding.GetChars(bytes, 0, byteCount, chars, 0);
for (int i = 0; i < charCount; i++)
{
var c = chars[i];
if (c == Char.MinValue)
{
var offset = encoder.GetByteCount(chars, 0, i + 1, true) - byteCount;
_stream.Seek(offset, SeekOrigin.Current);
isTerminated = true;
break;
}
text.Add(c);
}
if (!isTerminated && byteCount < _SIZE)
{
isTerminated = true;
}
}
return new String(text.ToArray());
}
示例14: UUGetString
public static string UUGetString(this byte[] obj, int index, int size, Encoding encoding)
{
string result = string.Empty;
if (obj != null && obj.Length >= (index + size))
{
int endIndex = index + size;
int dataLength = obj.Length;
int realSize = 0;
for (int i = index; i < dataLength && i < endIndex && obj[i] != 0; i++)
{
++realSize;
}
byte[] subData = obj.UUGetBytes(index, realSize);
result = new string(encoding.GetChars(subData));
}
return result;
}
示例15: GetCharsTestAutoBuffer
private void GetCharsTestAutoBuffer(Encoding encoding, byte[] inputBuffer, int inputIndex, int inputCount, int outputIndex, string expectedOutput, int expectedOutputCount)
{
var outputBuffer = new char[encoding.GetCharCount(inputBuffer, inputIndex, inputCount)];
var result = encoding.GetChars(inputBuffer, inputIndex, inputCount, outputBuffer, outputIndex == COUNT_AUTO ? 0 : outputIndex);
var actual = new string(outputBuffer);
Assert.Equal(expectedOutputCount, result);
Assert.Equal(expectedOutput, actual);
}