本文整理汇总了C#中System.Text.Encoding.GetByteCount方法的典型用法代码示例。如果您正苦于以下问题:C# Encoding.GetByteCount方法的具体用法?C# Encoding.GetByteCount怎么用?C# Encoding.GetByteCount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Text.Encoding
的用法示例。
在下文中一共展示了Encoding.GetByteCount方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteToStream
public void WriteToStream(Stream outputStream, Encoding encoding)
{
outputStream.WriteByte((Byte)'P');
// message length =
// Int32 self
// name of prepared statement + 1 null string terminator +
// query string + 1 null string terminator
// + Int16
// + Int32 * number of parameters.
Int32 messageLength = 4 + encoding.GetByteCount(_prepareName) + 1 + encoding.GetByteCount(_queryString) + 1 + 2 + (_parameterIDs.Length * 4);
//Int32 messageLength = 4 + _prepareName.Length + 1 + _queryString.Length + 1 + 2 + (_parameterIDs.Length * 4);
PGUtil.WriteInt32(outputStream, messageLength);
PGUtil.WriteString(_prepareName, outputStream, encoding);
PGUtil.WriteString(_queryString, outputStream, encoding);
PGUtil.WriteInt16(outputStream, (Int16)_parameterIDs.Length);
for(Int32 i = 0; i < _parameterIDs.Length; i++)
PGUtil.WriteInt32(outputStream, _parameterIDs[i]);
}
示例2: WriteToStream
public void WriteToStream(Stream outputStream, Encoding encoding)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "WriteToStream");
switch (protocolVersion) {
case ProtocolVersion.Version2 :
// Write the size of the packet.
// 4 + (passwordlength + 1) -> Int32 + NULL terminated string.
// output_stream.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(4 + (password.Length + 1))), 0, 4);
PGUtil.WriteInt32(outputStream, 4 + encoding.GetByteCount(password) + 1);
// Write String.
PGUtil.WriteString(password, outputStream, encoding);
break;
case ProtocolVersion.Version3 :
outputStream.WriteByte((Byte)'p');
PGUtil.WriteInt32(outputStream, 4 + encoding.GetByteCount(password) + 1);
// Write String.
PGUtil.WriteString(password, outputStream, encoding);
break;
}
}
示例3: GetByteLength
public override int GetByteLength(Encoding encoding)
{
int byteLength = 0;
byteLength += encoding.GetByteCount(indicator);
foreach (SubField sf in subfields)
{
byteLength += encoding.GetByteCount(Field.SubFieldStart.ToString());
byteLength += sf.GetByteLength(encoding);
}
byteLength += encoding.GetByteCount(Field.FieldEnd.ToString());
return byteLength;
}
示例4:
public static string PadRightInBytes
(
this string text
, int totalWidth
, char paddingChar = ' '
, Encoding encoding = null
)
{
if (encoding == null)
{
encoding = Encoding.GetEncoding("gb2312");
}
totalWidth -=
(
encoding.GetByteCount(text)
- text.Length
);
return
text
.PadRight
(
totalWidth
, paddingChar
);
}
示例5: GetByteCount
public unsafe static int GetByteCount(Encoding encoding, char[] chars, int index, int count)
{
// Validate parameters
Contract.Assert(encoding != null); // this parameter should only be affected internally, so just do a debug check here
if (chars == null)
{
throw new ArgumentNullException("chars", Environment.GetResourceString("ArgumentNull_Array"));
}
if (index < 0 || count < 0)
{
throw new ArgumentOutOfRangeException(index < 0 ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
if (chars.Length - index < count)
{
throw new ArgumentOutOfRangeException("chars", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
}
Contract.EndContractBlock();
// If no input, return 0, avoid fixed empty array problem
if (count == 0)
return 0;
// Just call the (internal) pointer version
fixed (char* pChars = chars)
return encoding.GetByteCount(pChars + index, count, encoder: null);
}
示例6: StringStream
public StringStream(string str, Encoding encoding = null)
{
_string = str ?? string.Empty;
_encoding = encoding ?? Encoding.UTF8;
_byteLength = _encoding.GetByteCount(_string);
}
示例7: WriteToStream
public void WriteToStream( Stream outputStream, Encoding encoding )
{
//NpgsqlEventLog.LogMsg( this.ToString() + _commandText, LogLevel.Debug );
String commandText = _command.GetCommandText();
// Tell to mediator what command is being sent.
_command.Connector.Mediator.SqlSent = commandText;
// Send the query to server.
// Write the byte 'Q' to identify a query message.
outputStream.WriteByte((Byte)'Q');
if (_protocolVersion == ProtocolVersion.Version3)
{
// Write message length. Int32 + string length + null terminator.
PGUtil.WriteInt32(outputStream, 4 + encoding.GetByteCount(commandText) + 1);
}
// Write the query. In this case it is the CommandText text.
// It is a string terminated by a C NULL character.
PGUtil.WriteString(commandText, outputStream, encoding);
}
示例8: ReadStringInternalDynamic
internal static string ReadStringInternalDynamic(this Stream stream, Encoding encoding, char end)
{
int characterSize = encoding.GetByteCount("e");
Debug.Assert(characterSize == 1 || characterSize == 2 || characterSize == 4);
string characterEnd = end.ToString();
int i = 0;
byte[] data = new byte[128 * characterSize];
while (true)
{
if (i + characterSize > data.Length)
{
Array.Resize(ref data, data.Length + (128 * characterSize));
}
int read = stream.Read(data, i, characterSize);
Debug.Assert(read == characterSize);
if (encoding.GetString(data, i, characterSize) == characterEnd)
{
break;
}
i += characterSize;
}
if (i == 0)
{
return "";
}
return encoding.GetString(data, 0, i);
}
示例9: MetaData
public MetaData(Roles role, Actions action, ContentTypes contentType, Encoding encoding, string message)
{
this.role = role;
this.action = action;
this.contentType = contentType;
this.encoding = encoding;
messageSize = encoding.GetByteCount(message);
}
示例10: ReadStringData
/// <summary>
/// Reads the string data.
/// </summary>
/// <param name="binaryReader">The binary reader.</param>
/// <param name="encoding">The encoding.</param>
/// <returns></returns>
public static string ReadStringData(this BinaryReader binaryReader, Encoding encoding)
{
int size = binaryReader.ReadUInt16();
return encoding.GetString(
binaryReader.ReadBytes(
size * encoding.GetByteCount(" ")));
}
示例11: WriteNullTermString
public static void WriteNullTermString(this Stream stream, string value, Encoding encoding)
{
var dataLength = encoding.GetByteCount(value);
var data = new byte[dataLength + 1];
encoding.GetBytes(value, 0, value.Length, data, 0);
data[dataLength] = 0x00; // '\0'
stream.Write(data, 0, data.Length);
}
示例12: 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());
}
示例13: GetByteCount
/// <summary>
/// Gets the number of bytes for specified string in given encoding.
/// </summary>
/// <param name="encoding">The encoding.</param>
/// <param name="value">The value.</param>
/// <returns></returns>
public static int GetByteCount(Encoding encoding, string value)
{
if (encoding == null) {
throw new ArgumentNullException("encoding");
}
if (value == null) {
throw new ArgumentNullException("value");
}
return encoding.GetByteCount(value);
}
示例14: Create
public static unsafe IntPtr Create(string text, Encoding encoding)
{
// Avoid unnecessary allocations by allocating the native buffer directly in managed code
var byteCount = encoding.GetByteCount(text);
var bytesPointer = (byte*)Marshal.AllocHGlobal(byteCount);
fixed (char* charsPointer = text)
encoding.GetBytes(charsPointer, text.Length, bytesPointer, byteCount);
return SafeNativeMethods.objc_msgSend(Class, Selectors.DataWithBytesNoCopyLengthFreeWhenDone, (IntPtr)bytesPointer, checked((IntPtr)byteCount), true);
}
示例15: StringToPtr
public static IntPtr StringToPtr(string value, Encoding encoding)
{
var encoder = encoding.GetEncoder();
var length = encoding.GetByteCount(value);
// The encoded value is null terminated that's the reason for the '+1'.
var encodedValue = new byte[length + 1];
encoding.GetBytes(value, 0, value.Length, encodedValue, 0);
var handle = Marshal.AllocHGlobal(new IntPtr(encodedValue.Length));
Marshal.Copy(encodedValue, 0, handle, encodedValue.Length);
return handle;
}