sockscape/server/Libraries/Square/StringExtensions.cs

34 lines
1.2 KiB
C#
Raw Normal View History

2017-05-08 21:06:17 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Square {
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-05-08 21:06:17 +00:00
}
}