DutyContent/ThirdParty/Converter.cs

157 lines
2.4 KiB
C#
Raw Normal View History

2021-03-20 19:43:05 +01:00
using System;
using System.Drawing;
2021-03-27 14:34:19 +01:00
using System.Net;
2021-03-20 19:43:05 +01:00
namespace DutyContent.ThirdParty
2021-03-17 18:14:48 +01:00
{
static class Converter
{
2021-03-20 19:43:05 +01:00
public static bool TryLong(string s, out long ret)
{
ret = 0;
for (int i = 0; i < s.Length; i++)
{
var n = s[i] - '0';
if (n < 0 || n > 9)
return false;
ret = ret * 10 + n;
}
return true;
}
public static long ToLong(string s, long failret = 0)
{
long ret = 0;
for (int i = 0; i < s.Length; i++)
{
var n = s[i] - '0';
if (n < 0 || n > 9)
return failret;
ret = ret * 10 + n;
}
return ret;
}
2021-03-17 18:14:48 +01:00
public static bool TryInt(string s, out int ret)
{
ret = 0;
for (int i = 0; i < s.Length; i++)
{
var n = s[i] - '0';
if (n < 0 || n > 9)
return false;
ret = ret * 10 + n;
}
return true;
}
public static int ToInt(string s, int failret = 0)
{
int ret = 0;
for (int i = 0; i < s.Length; i++)
{
var n = s[i] - '0';
if (n < 0 || n > 9)
return failret;
ret = ret * 10 + n;
}
return ret;
}
public static bool TryUshort(string s, out ushort ret)
{
ret = 0;
for (int i = 0; i < s.Length; i++)
{
var n = s[i] - '0';
if (n < 0 || n > 9)
return false;
ret = (ushort)(ret * 10 + n);
}
return true;
}
2021-03-20 19:43:05 +01:00
public static ushort ToUshort(string s, ushort failret = 0)
2021-03-17 18:14:48 +01:00
{
int ret = 0;
for (int i = 0; i < s.Length; i++)
{
var n = s[i] - '0';
if (n < 0 || n > 9)
return failret;
ret = ret * 10 + n;
}
return (ushort)ret;
}
public static bool ToBool(string s)
{
return s.ToUpper().Equals("TRUE");
}
2021-03-20 19:43:05 +01:00
public static float ToFloat(string s, float failret = 0.0f)
2021-03-17 18:14:48 +01:00
{
return float.TryParse(s, out float v) ? v : failret;
}
2021-03-20 19:43:05 +01:00
public static Color ToColorArgb(string s, Color failret)
{
try
{
var i = Convert.ToInt32(s, 16);
var r = Color.FromArgb(i);
return r;
}
catch
{
return failret;
}
}
public static Color ToColorArgb(string s)
{
return ToColorArgb(s, Color.Transparent);
}
2021-03-27 14:34:19 +01:00
public static IPAddress ToIPAddressFromIPV4(string ipstr)
{
try
{
var sa = ipstr.Trim().Split('.');
if (sa.Length == 4)
{
if (sa[3].Contains(":"))
sa[3] = sa[3].Substring(0, sa[3].IndexOf(":"));
var ivs = new byte[4];
for (var i = 0; i < 4; i++)
ivs[i] = byte.Parse(sa[i]);
return new IPAddress(ivs);
}
}
catch { }
return IPAddress.None;
}
2021-03-17 18:14:48 +01:00
}
}