本文整理汇总了C#中System.Text.Encoding.GetBytes方法的典型用法代码示例。如果您正苦于以下问题:C# Encoding.GetBytes方法的具体用法?C# Encoding.GetBytes怎么用?C# Encoding.GetBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Text.Encoding
的用法示例。
在下文中一共展示了Encoding.GetBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessCommand
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
{
}
}
示例2: BoundaryStreamReader
public BoundaryStreamReader(string boundary, Stream baseStream, Encoding streamEncoding, int bufferLength)
{
if (baseStream == null)
{
throw new ArgumentNullException("baseStream");
}
if (!baseStream.CanSeek || !baseStream.CanRead)
{
throw new ArgumentException("baseStream must be a seekable readable stream.");
}
if (bufferLength < boundary.Length + 6)
{
throw new ArgumentOutOfRangeException(
"bufferLength",
"The buffer needs to be big enough to contain the boundary and control characters (6 bytes)");
}
this.Log = NullLogger<IOLogSource>.Instance;
this.BaseStream = baseStream;
// by default if unspecified an encoding should be ascii
// some people are of the opinion that utf-8 should be parsed by default
// or that it should depend on the source page.
// Need to test what browsers do in the wild.
Encoding = streamEncoding;
Encoding.GetBytes("--" + boundary);
this.beginBoundary = Encoding.GetBytes("\r\n--" + boundary);
this.localBuffer = new byte[bufferLength];
this.beginBoundaryAsString = "--" + boundary;
this.AtPreamble = true;
}
示例3: Write
//--- Extension Methods ---
/// <summary>
/// Write a string to <see cref="Stream"/>
/// </summary>
/// <param name="stream">Target <see cref="Stream"/></param>
/// <param name="encoding">Encoding to use to convert the string to bytes</param>
/// <param name="text">Regular string or composite format string to write to the <see cref="Stream"/></param>
/// <param name="args">An System.Object array containing zero or more objects to format.</param>
public static void Write(this Stream stream, Encoding encoding, string text, params object[] args) {
const int bufferSize = BUFFER_SIZE / sizeof(char);
if(text.Length > bufferSize) {
// to avoid a allocating a byte array of greater than 64k, we chunk our string writing here
if(args.Length != 0) {
text = string.Format(text, args);
}
var length = text.Length;
var idx = 0;
var buffer = new char[bufferSize];
while(true) {
var size = Math.Min(bufferSize, length - idx);
if(size == 0) {
break;
}
text.CopyTo(idx, buffer, 0, size);
stream.Write(encoding.GetBytes(buffer, 0, size));
idx += size;
}
} else {
if(args.Length == 0) {
stream.Write(encoding.GetBytes(text));
} else {
stream.Write(encoding.GetBytes(string.Format(text, args)));
}
}
}
示例4: Encrypt
/// <summary>
/// aes 加密
/// </summary>
/// <param name="toEncrypt"></param>
/// <param name="key"></param>
/// <param name="encoding">加密编码方式 默认为 utf-8 </param>
/// <returns></returns>
public static string Encrypt(string toEncrypt, string key, Encoding encoding = null)
{
string result = string.Empty;
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException("key", "key值不能为空");
if (string.IsNullOrEmpty(toEncrypt))
return result;
if (encoding==null)
encoding=Encoding.UTF8;
try
{
byte[] keyArray = encoding.GetBytes(key);// ToByte(key);
byte[] toEncryptArray = encoding.GetBytes(toEncrypt);
var resultArray = Encrypt(keyArray, toEncryptArray);
result = Convert.ToBase64String(resultArray);
}
catch
{
}
return result;
}
示例5: MultipartWriter
public MultipartWriter(string boundary, Stream underlyingStream, Encoding encoding)
{
_boundary = boundary;
_underlyingStream = underlyingStream;
_encoding = encoding;
_beginBoundary = encoding.GetBytes("--" + boundary +"\r\n" );
_endBoundary = encoding.GetBytes("\r\n--" + boundary + "--\r\n");
}
示例6: ParseView
public byte[] ParseView(IView view, string viewTemplate, Encoding encoding)
{
if (File.Exists(viewTemplate))
{
return encoding.GetBytes(File.ReadAllText(viewTemplate));
}
return encoding.GetBytes(viewTemplate + " not found");
}
示例7: MultipartWriter
public MultipartWriter(string boundary, Stream underlyingStream, Encoding encoding)
{
this.boundary = boundary;
this.underlyingStream = underlyingStream;
this.encoding = encoding;
this.beginBoundary = encoding.GetBytes("--" + boundary + "\r\n");
this.endBoundary = encoding.GetBytes("\r\n--" + boundary + "--\r\n");
}
示例8: HMACMD5
/// <summary>
/// Creates an HMAC-MD5 fingerprint of the given data with the given key using the specified encoding
/// </summary>
/// <param name="data"></param>
/// <param name="key"></param>
/// <param name="enc"></param>
/// <returns></returns>
public static string HMACMD5(this string data, string key, Encoding enc)
{
var hmacKey = enc.GetBytes(key);
var hmacData = enc.GetBytes(data);
using (var hmacMd5 = new HMACMD5(hmacKey)) {
return hmacMd5.ComputeHash(hmacData).ToHex().ToLower();
}
}
示例9: StaticContentFilter
public StaticContentFilter(HttpResponse response, string imagePrefix, string javascriptPrefix, string cssPrefix)
{
this._Encoding = response.Output.Encoding;
this._ResponseStream = response.Filter;
this._ImagePrefix = _Encoding.GetBytes(imagePrefix);
this._JavascriptPrefix = _Encoding.GetBytes(javascriptPrefix);
this._CssPrefix = _Encoding.GetBytes(cssPrefix);
}
示例10: CompressToByte
public static byte[] CompressToByte(char[] str, bool needheadflag, Encoding enc)
{
if (str.Length < zipsizemin) return enc.GetBytes(str);
Byte[] bTytes = enc.GetBytes(str);
Byte[] retbytes = Compress(bTytes, needheadflag, enc);
return retbytes;
}
示例11: Authenticate
public static int Authenticate(string user, string pass, Stream status, Encoding encoding)
{
var passhash = Sha1Hash(pass);
byte[] b;
if (Config.UserCache[user + "§custom"] == "true")
{
b = encoding.GetBytes("\u001B[36mAuthenticating against local hash...\u001B[0m\r\n");
status.Write(b, 0, b.Length);
if (passhash != Config.UserCache[user + "§hash"])
{
return -1;
}
int seclvlc;
var prc = int.TryParse(Config.UserCache[user], out seclvlc);
return prc ? seclvlc : 0;
}
b = encoding.GetBytes("\u001B[36mAuthenticating with minecraft.net\r\n");
status.Write(b, 0, b.Length);
var olresult = ValidateOnline(user, pass);
if (olresult == null)
{
b=encoding.GetBytes("\u001B[31mError connecting to minecraft.net\r\n");
status.Write(b, 0, b.Length);
b = encoding.GetBytes("\u001B[36mAuthenticating against local cache...\u001B[0m\r\n");
status.Write(b, 0, b.Length);
if (Config.UserCache[user + "§hash"] != "")
{
if (passhash == Config.UserCache[user + "§hash"])
{
olresult = true;
}
else return -1;
}
else
{
b = encoding.GetBytes("\u001B[31mPassword not availible in cache\u001B[0m\r\n");
status.Write(b, 0, b.Length);
return -1;
}
}
if (olresult == false) return -1;
Config.UserCache[user + "§hash"] = passhash;
int seclvl;
var pr = int.TryParse(Config.UserCache[user], out seclvl);
return pr ? seclvl : 0;
}
示例12: WriteStringInternalDynamic
internal static void WriteStringInternalDynamic(this Stream stream, Encoding encoding, string value, char end)
{
byte[] data;
data = encoding.GetBytes(value);
stream.Write(data, 0, data.Length);
data = encoding.GetBytes(end.ToString());
stream.Write(data, 0, data.Length);
}
示例13: DoEncrypt
/// <summary>
///
/// </summary>
/// <param name="plainText"></param>
/// <param name="key"></param>
/// <param name="encoding"></param>
/// <param name="encryptedType"></param>
/// <returns></returns>
public override string DoEncrypt(string plainText, string key, Encoding encoding, DataMode encryptedType)
{
byte[] keyByte = encoding.GetBytes(key);
HMACMD5 hmacMD5 = new HMACMD5(keyByte);
byte[] messageBytes = encoding.GetBytes(plainText);
byte[] hashMessage = hmacMD5.ComputeHash(messageBytes);
return BytesToString(hashMessage, encoding, encryptedType);
}
示例14: StreamingMultiPartParser
private StreamingMultiPartParser(Stream bodyStream, Encoding encoding, string boundary, byte[] outerBoundary, Buffer buffer)
{
this.bodyStream = bodyStream;
this.encoding = encoding;
this.boundary = encoding.GetBytes("--" + boundary);
this.outerBoundary = outerBoundary;
this.dashes = encoding.GetBytes("--");
this.newLine = encoding.GetBytes("\r\n");
this.buffer = buffer;
}
示例15: CheckCharCode
/// <summary>コンストラクタ</summary>
public CheckCharCode(string startChar, string endChar, Encoding stringEncoding)
{
this.StartChar = startChar;
this.EndChar = endChar;
this.StringEncoding = stringEncoding;
// 1文字のバイトデータを数値データ(long)に変換
this.StartCode = PubCmnFunction.GetLongFromByte(stringEncoding.GetBytes(startChar));
this.EndCode = PubCmnFunction.GetLongFromByte(stringEncoding.GetBytes(endChar));
}