2017-05-08 21:06:17 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2017-07-22 19:27:41 +00:00
|
|
|
|
namespace Glove {
|
2017-09-07 21:00:03 +00:00
|
|
|
|
public static class CharExtensions {
|
|
|
|
|
public static bool IsHex(this char c) {
|
|
|
|
|
return (c >= '0' && c <= '9')
|
|
|
|
|
|| (c >= 'A' && c <= 'F')
|
|
|
|
|
|| (c >= 'a' && c <= 'f');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static byte HexValue(this char c) {
|
|
|
|
|
return (byte)(!c.IsHex() ? 0 : c - (c < 0x3A ? 0x30 : (c < 0x61 ? 0x37 : 0x57)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 21:06:17 +00:00
|
|
|
|
public static class StringExtensions {
|
2017-05-20 23:33:39 +00:00
|
|
|
|
public static byte[] GetBytes(this string str, bool isUtf8 = true)
|
|
|
|
|
=> isUtf8 ? Encoding.UTF8.GetBytes(str)
|
|
|
|
|
: Encoding.ASCII.GetBytes(str);
|
2017-05-08 21:06:17 +00:00
|
|
|
|
|
2017-05-20 23:33:39 +00:00
|
|
|
|
public static int ByteLength(this string str, bool isUtf8 = true)
|
|
|
|
|
=> isUtf8 ? Encoding.UTF8.GetByteCount(str)
|
|
|
|
|
: Encoding.ASCII.GetByteCount(str);
|
2017-05-08 21:06:17 +00:00
|
|
|
|
|
|
|
|
|
public static string Base64Encode(this string str, bool isUtf8 = true) {
|
|
|
|
|
var raw =
|
|
|
|
|
isUtf8 ? Encoding.UTF8.GetBytes(str)
|
|
|
|
|
: Encoding.ASCII.GetBytes(str);
|
|
|
|
|
return Convert.ToBase64String(raw);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string Base64Decode(this string str, bool isUtf8 = true) {
|
|
|
|
|
var raw = Convert.FromBase64String(str);
|
|
|
|
|
return isUtf8 ? Encoding.UTF8.GetString(raw)
|
|
|
|
|
: Encoding.ASCII.GetString(raw);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-20 23:33:39 +00:00
|
|
|
|
public static byte[] Base64DecodeRaw(this string str)
|
|
|
|
|
=> Convert.FromBase64String(str);
|
2017-06-07 21:02:29 +00:00
|
|
|
|
|
|
|
|
|
public static bool StartsWith(this string str, params string[] strings) {
|
|
|
|
|
foreach(var checkString in strings) {
|
|
|
|
|
if(str.StartsWith(checkString))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-05-08 21:06:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|