2026-06-05 13:44:39 +09:00

143 lines
4.3 KiB
C#

using System;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
namespace AuroraDeviceCommon.Utils
{
public static class Bit
{
public const bool Off = false;
public const bool On = true;
}
public static class Utils
{
#region 글로변 변수
public const double R2D = 180.0 / Math.PI;
public const double D2R = Math.PI / 180.0;
public const double Hecto = 100;
public const double Kilo = 1000;
public const double Milli = 1e-3;
public const double EPS2 = 1e-2;
public const double EPS3 = 1e-3;
public const double EPS4 = 1e-4;
public const double EPS7 = 1e-7;
#endregion
public static string WriteLogTag(in string message, [CallerMemberName] string memberName = "", [CallerLineNumber] int line = 0)
{
return $"{message} ({memberName}:{line.ToString()})";
}
public static bool IsNumeric(ref string strValue, bool isHex = false)
{
// ^ : 라인의 처음
// $ : 라인의 마지막
// [-+]? : 문자열 맨 앞에 + 문자와 - 문자를 허용
// \d : 숫자
// * : zero 혹은 그 이상
// \.\d+ : 0 ~ 1 개의 소수점을 허용
if (isHex != true) {
// +- 및 숫자와 소수점 1개까지만 허용
// +-숫자만 | +-숫자 + 소수점
// .1 은 허용하지만, 1. 은 허용안함.. 소수점이 있을 경우, 뒤에 무조건 숫자가 1개이상 있어야함
return Regex.IsMatch(strValue, @"^[-+]?\d*(\.?\d+)$");
//return double.TryParse(strValue, out double tmp);
} else {
// +- 및 문자(a ~ f)와 숫자만 허용 (소수점 불가)
return Regex.IsMatch(strValue, @"^[-+]?[a-fA-F0-9]+$");
//return double.TryParse(strValue, System.Globalization.NumberStyles.AllowHexSpecifier, null, out double tmp);
}
}
public static short Hex2short(string value)
{
return short.Parse(value, System.Globalization.NumberStyles.AllowHexSpecifier);
}
public static string StrLeft(string value, int length)
{
string tempStr;
if (string.IsNullOrEmpty(value)) {
return string.Empty;
} else {
tempStr = value.Substring(0, length);
}
return tempStr;
}
public static int Hex2Int(string value)
{
return int.Parse(value, System.Globalization.NumberStyles.AllowHexSpecifier);
}
public static int Hex2Int(string value, out bool bRet)
{
try {
int iTemp = int.Parse(value, System.Globalization.NumberStyles.AllowHexSpecifier);
bRet = true;
return iTemp;
} catch {
bRet = false;
return 0;
}
}
public static string StrRight(string value, int length)
{
string tempStr;
if (string.IsNullOrEmpty(value)) {
return "";
} else {
tempStr = value.Substring(value.Length - length);
}
return tempStr;
}
/*
public static string FillZeroString(string strData, int intLength)
{
StringBuilder sdTemp = new StringBuilder();
sdTemp.Clear();
sdTemp.Append(strData);
// 0 을 앞자리에다가 채우도록 수정 필요..
for (int i = sdTemp.Length; i < intLength; i++) {
sdTemp.Append("0");
}
return sdTemp.ToString();
}
*/
public static bool IsEqual(float val1, float val2, int unit = 5)
{
var compareUnit = Math.Pow(0.1, unit);
if (Math.Abs(val1 - val2) < compareUnit) {
return true;
} else {
return false;
}
}
public static bool IsEqual(double val1, double val2, int unit = 13)
{
var compareUnit = Math.Pow(0.1, unit);
if (Math.Abs(val1 - val2) < compareUnit) {
return true;
} else {
return false;
}
}
}
}