System.Text.Encoding.GetBytes C# (CSharp) Метод

GetBytes() приватный Метод

private GetBytes ( char chars, int charCount, byte bytes, int byteCount ) : int
chars char
charCount int
bytes byte
byteCount int
Результат int
        public virtual unsafe int GetBytes(char* chars, int charCount,
                                              byte* bytes, int byteCount)
        {
            // Validate input parameters
            if (bytes == null || chars == null)
                throw new ArgumentNullException(bytes == null ? "bytes" : "chars",
                    Environment.GetResourceString("ArgumentNull_Array"));

            if (charCount < 0 || byteCount < 0)
                throw new ArgumentOutOfRangeException((charCount<0 ? "charCount" : "byteCount"),
                    Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));

            // Get the char array to convert
            char[] arrChar = new char[charCount];

            int index;
            for (index = 0; index < charCount; index++)
                arrChar[index] = chars[index];

            // Get the byte array to fill
            byte[] arrByte = new byte[byteCount];

            // Do the work
            int result = GetBytes(arrChar, 0, charCount, arrByte, 0);

            // The only way this could fail is a bug in GetBytes
            BCLDebug.Assert(result <= byteCount, "[Encoding.GetBytes]Returned more bytes than we have space for");

            // Copy the byte array
            // WARNING: We MUST make sure that we don't copy too many bytes.  We can't
            // rely on result because it could be a 3rd party implimentation.  We need
            // to make sure we never copy more than byteCount bytes no matter the value
            // of result
            if (result < byteCount)
                byteCount = result;

            // Copy the data, don't overrun our array!
            for (index = 0; index < byteCount; index++)
                bytes[index] = arrByte[index];

            return byteCount;
        }

Same methods

Encoding::GetBytes ( String s ) : byte[]
Encoding::GetBytes ( char chars ) : byte[]
Encoding::GetBytes ( char chars, int index, int count ) : byte[]
Encoding::GetBytes ( String s, int charIndex, int charCount, byte bytes, int byteIndex ) : int
Encoding::GetBytes ( char chars, int charCount, byte bytes, int byteCount, EncoderNLS encoder ) : int
Encoding::GetBytes ( char chars, int charIndex, int charCount, byte bytes, int byteIndex ) : int

Usage Example

        public static void ProcessCommand(string command, Stream io, int security, string user, Encoding encoding)
        {
            if (String.IsNullOrWhiteSpace(command))
            {
                return;
            }

            try
            {
                byte[] writebuffer;

                var cmdsplit = command.Split(' ');
                if (cmdsplit.Length < 1) return;
                var label = cmdsplit[0];

                if (SBWAPI.PluginManager.Default.ProcessCommand(label, command, io, security, user, encoding)) return;

                if (command.StartsWith("#"))
                {
                    if (security < 4)
                    {
                        writebuffer =
                            encoding.GetBytes(
                                "\u001B[31mYou do not have permission to send special commands\u001B[0m\r\n");
                        io.Write(writebuffer, 0, writebuffer.Length);
                        return;
                    }

                    if (CommandProcessors.ContainsKey(label))
                        CommandProcessors[label](command, io, security, user, encoding);
                    return;
                }
                if (command.StartsWith("/"))
                {
                    if (security < 3)
                    {
                        writebuffer = encoding.GetBytes(
                            "\u001B[31mYou do not have permission to send commands\u001B[0m\r\n");
                        io.Write(writebuffer, 0, writebuffer.Length);
                        return;
                    }

                    if (CommandProcessors.ContainsKey(label))
                        CommandProcessors[label](command, io, security, user, encoding);
                    else
                    {
                        ServerHandler.ProcessHandler.ExtOutput(string.Format("<{0}> {1}", user, command));
                        ServerHandler.ProcessHandler.Instance.Command(command.Remove(0, 1));
                    }
                    return;
                }

                CommandProcessors["\uFFFF"](command, io, security, user, encoding);
            }
            catch
            {
            }
        }
All Usage Examples Of System.Text.Encoding::GetBytes