本文整理汇总了C#中System.Text.Encoding.GetCharCount方法的典型用法代码示例。如果您正苦于以下问题:C# Encoding.GetCharCount方法的具体用法?C# Encoding.GetCharCount怎么用?C# Encoding.GetCharCount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Text.Encoding
的用法示例。
在下文中一共展示了Encoding.GetCharCount方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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());
}
示例2: 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;
}
}
示例3: 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);
}
示例4: 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);
}
示例5: ByteToCharStyles
public static unsafe byte[] ByteToCharStyles(byte* styles, byte* text, int length, Encoding encoding)
{
// This is used by annotations and margins to get all the styles in one call.
// It converts an array of styles where each element corresponds to a BYTE
// to an array of styles where each element corresponds to a CHARACTER.
var bytePos = 0; // Position within text BYTES and style BYTES (should be the same)
var charPos = 0; // Position within style CHARACTERS
var decoder = encoding.GetDecoder();
var result = new byte[encoding.GetCharCount(text, length)];
while (bytePos < length)
{
if (decoder.GetCharCount(text + bytePos, 1, false) > 0)
result[charPos++] = *(styles + bytePos); // New char
bytePos++;
}
return result;
}
示例6: Escape
public static string Escape(string s, Encoding encoding, EscapeMode escapeMode)
{
StringBuilder accum = new StringBuilder(s.Length * 2);
Dictionary<char, string> map = escapeMode.GetMap();
for (int pos = 0; pos < s.Length; pos++)
{
char c = s[pos];
// To replicate this line from the original code: encoder.canEncode(c), we perform the following steps:
byte[] unicodeBytes = Encoding.Unicode.GetBytes(c.ToString()); // Get the bytes by unicode.
byte[] encodingBytes = Encoding.Convert(Encoding.Unicode, encoding, unicodeBytes); // Convert bytes into target encoding's bytes.
char[] encodingChars = new char[encoding.GetCharCount(encodingBytes)];
encoding.GetChars(encodingBytes, 0, encodingBytes.Length, encodingChars, 0); // Convert target encoding bytes into chars.
if (map.ContainsKey(c))
{
accum.AppendFormat("&{0};", map[c]);
}
else if (encodingChars[0] == c)
{
// If the char resulting from the conversion matches the original one, we can understand that the encoding can encode this char.
accum.Append(c); // If so, add it to the accumulator.
}
else
{
accum.AppendFormat("&#{0};", (int)c);
}
}
return accum.ToString();
}
示例7: ReadChars
private int ReadChars(byte* tmpBuffer, int byteCount, ref char[] outputArray, Encoding encoding)
{
ReadMemory(tmpBuffer, byteCount * 8);
int charCount = encoding.GetCharCount(tmpBuffer, byteCount);
if (charCount > outputArray.Length)
outputArray = new char[Math.Max(charCount, outputArray.Length * 2)];
fixed (char* chars = &outputArray[0])
{
encoding.GetChars(tmpBuffer, byteCount, chars, charCount);
}
return charCount;
}
示例8: ReadPrefixLengthString
public string ReadPrefixLengthString(Encoding encoding)
{
int byteCount = (int)ReadUInt32Variant();
if(byteCount == 0)
{
return String.Empty;
}
else if (byteCount <= 1024)
{
byte* bytes = stackalloc byte[byteCount];
ReadMemory(bytes, byteCount * 8);
int charCount = encoding.GetCharCount(bytes, byteCount);
char* chars = stackalloc char[charCount];
encoding.GetChars(bytes, byteCount, chars, charCount);
return new string(chars, 0, charCount);
}
else
{
// Heap alloc
byte[] data = new byte[byteCount];
fixed (byte* ptr = data)
{
ReadMemory(ptr, byteCount * 8);
}
return new string(encoding.GetChars(data));
}
}
示例9: GetCharCount
public static unsafe int GetCharCount(Encoding encoding, byte* bytes, int count)
{
Debug.Assert(encoding != null);
if (bytes == null)
{
throw new ArgumentNullException(nameof(bytes), SR.ArgumentNull_Array);
}
if (count < 0)
{
throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
}
Contract.EndContractBlock();
return encoding.GetCharCount(bytes, count, decoder: null);
}
示例10: GetCharCountTest
private void GetCharCountTest(Encoding encoding, byte[] buffer, int startIndex, int count, int expected)
{
var result = encoding.GetCharCount(buffer, startIndex, count);
Assert.Equal(expected, result);
}
示例11: GetCharCount
public unsafe static int GetCharCount(Encoding encoding, byte[] bytes, int index, int count)
{
Contract.Assert(encoding != null);
if (bytes == null)
{
throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
}
if (index < 0 || count < 0)
{
throw new ArgumentOutOfRangeException(index < 0 ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
if (bytes.Length - index < count)
{
throw new ArgumentOutOfRangeException("bytes", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
}
Contract.EndContractBlock();
// If no input just return 0, fixed doesn't like 0 length arrays.
if (count == 0)
return 0;
// Just call pointer version
fixed (byte* pBytes = bytes)
return encoding.GetCharCount(pBytes + index, count, decoder: null);
}
示例12: GetCharCount
/// <summary>
/// Gets the number of CHAR in a BYTE range
/// </summary>
private static unsafe int GetCharCount(IntPtr text, int length, Encoding encoding)
{
if (text == IntPtr.Zero || length == 0)
return 0;
var count = encoding.GetCharCount((byte*)text, length);
return count;
}
示例13: ReadString2
public static unsafe string ReadString2(BufferSegment buf, ref int offset, Encoding encoding)
{
int len = ReadUShort(buf, ref offset);
if (len == 0) {
return string.Empty;
}
if (encoding.GetCharCount(buf.Buffer.Array, buf.Offset + offset, len) == 0) {
return string.Empty;
}
string str = new string(encoding.GetChars(buf.Buffer.Array, buf.Offset + offset, len));
offset += len;
return str;
}
示例14: ReadString1
//public static string ReadString(Stream stream, int len, Encoding encoding) {
// byte[] buf = new byte[len];
// stream.Read(buf, 0, len);
// return encoding.GetString(buf);
//}
/// <summary>
/// 把 1位 的长度也读进来了
/// </summary>
/// <param name="stream"></param>
/// <param name="len"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static unsafe string ReadString1(byte* buf, ref int offset, Encoding encoding)
{
int len = ReadByte(buf, ref offset);
//byte[] temp = new byte[len];
////fixed (byte* bb = temp) {
//// byte* cc = bb;
//// for (int i = 0; i < len; i++) {
//// *cc = *(buf + offset);
//// cc++;
//// offset++;
//// }
////}
//Marshal.Copy((IntPtr)(void*)(buf + offset), temp, 0, len);
//offset += len;
//string s = encoding.GetString(temp);
//return s;
if (len == 0) {
return string.Empty;
}
int length = encoding.GetCharCount(buf + offset, len);
if (length == 0) {
return string.Empty;
}
string str = new string(' ', length);
fixed (char* chRef = str) {
encoding.GetChars(buf + offset, len, chRef, length);
}
offset += len;
return str;
}
示例15: ReadString
public static unsafe string ReadString(BufferSegment buf, ref int offset, int len, Encoding encoding)
{
//
if (encoding.GetCharCount(buf.Buffer.Array, buf.Offset + offset, len) == 0) {
return string.Empty;
}
// string str = new string(' ', length);
//fixed (char* chRef = str) {
char[] chars = encoding.GetChars(buf.Buffer.Array, buf.Offset + offset, len);
//}
string str = new string(chars);
offset += len;
return str;
}