Class BTS
- Namespace
- SunamoBts
- Assembly
- SunamoBts.dll
Basic Type System utilities for parsing and converting between primitive types.
public class BTS
- Inheritance
-
BTS
- Inherited Members
- Extension Methods
Properties
LastBool
Last successfully parsed bool value.
public static bool LastBool { get; set; }
Property Value
LastByte
Last successfully parsed byte value.
public static byte LastByte { get; set; }
Property Value
LastDateTime
Last successfully parsed DateTime value.
public static DateTime LastDateTime { get; set; }
Property Value
LastDouble
Last successfully parsed double value.
public static double LastDouble { get; set; }
Property Value
LastFloat
Last successfully parsed float value.
public static float LastFloat { get; set; }
Property Value
LastInt
Last successfully parsed int value.
public static int LastInt { get; set; }
Property Value
LastLong
Last successfully parsed long value.
public static long LastLong { get; set; }
Property Value
LastUint
Stores the last successfully parsed uint value from TryParseUint.
public static uint LastUint { get; set; }
Property Value
Methods
BoolToInt(bool)
Converts a boolean value to an integer (1 for true, 0 for false).
public static int BoolToInt(bool value)
Parameters
valueboolThe boolean value to convert.
Returns
- int
1 if true, 0 if false.
BoolToString(bool)
Returns Czech string representation for the bool value (Ano/Ne).
public static string BoolToString(bool value)
Parameters
valueboolThe boolean value to convert.
Returns
- string
"Ano" for true, "Ne" for false.
BoolToString(bool, bool)
Converts a boolean value to its English string representation (Yes/No).
public static string BoolToString(bool value, bool isLowerCase = false)
Parameters
valueboolThe boolean value to convert.
isLowerCaseboolIf true, returns lowercase representation; otherwise, returns capitalized representation.
Returns
- string
String representation of the boolean value in English.
BoolToStringEn(bool, bool)
Converts a boolean value to its English string representation (Yes/No). Delegates to BoolToString(bool, bool).
public static string BoolToStringEn(bool value, bool isLowerCase = false)
Parameters
valueboolThe boolean value to convert.
isLowerCaseboolIf true, returns lowercase representation; otherwise, returns capitalized representation.
Returns
- string
String representation of the boolean value in English.
CastArrayIntToString(int[])
Converts an array of integers to a list of strings.
public static List<string> CastArrayIntToString(int[] array)
Parameters
arrayint[]The array of integers to convert.
Returns
CastArrayObjectToString(object[])
Converts an array of objects to a list of strings.
public static List<string> CastArrayObjectToString(object[] array)
Parameters
arrayobject[]The array of objects to convert.
Returns
CastCollectionIntToShort(List<int>)
Converts a list of integers to a list of shorts.
public static List<short> CastCollectionIntToShort(List<int> list)
Parameters
Returns
CastCollectionShortToInt(List<short>)
Casts a collection of shorts to a list of integers. Before use you can call RemoveNotNumber to avoid raising an exception.
public static List<int> CastCollectionShortToInt(List<short> list)
Parameters
Returns
CastCollectionStringToInt(IList<string>)
Casts a collection of strings to a list of integers. Throws an exception if any value cannot be cast to int. Before use you can call RemoveNotNumber to avoid raising an exception.
public static List<int> CastCollectionStringToInt(IList<string> list)
Parameters
Returns
CastListShortToListInt(List<short>)
Casts a list of shorts to a list of integers. Before use you can call RemoveNotNumber to avoid raising an exception.
public static List<int> CastListShortToListInt(List<short> list)
Parameters
Returns
CastToByT<T>(string, bool)
Casts a string to the specified generic type T, optionally treating it as a character.
public static T CastToByT<T>(string text, bool isChar)
Parameters
textstringThe string text to cast.
isCharboolIf true, casts the first character; otherwise, casts the entire string.
Returns
- T
The casted value of type T.
Type Parameters
TThe target type to cast to.
CastToIntList<U>(IList<U>)
Casts a generic list to a list of integers.
public static List<int> CastToIntList<U>(IList<U> list)
Parameters
listIList<U>The list to convert.
Returns
Type Parameters
UThe type of elements in the source list.
ClearEndingsBytes(List<byte>)
Removes trailing zero bytes from the list.
public static List<byte> ClearEndingsBytes(List<byte> list)
Parameters
Returns
CompareAsObjectAndString(object, object)
Compares two objects by reference and by their string representation. Checks for null in secondObject before comparison.
public static bool CompareAsObjectAndString(object firstObject, object secondObject)
Parameters
Returns
- bool
True if objects are equal by reference or string representation; otherwise, false.
ConvertFromBytesToUtf8(List<byte>)
Converts a list of bytes to a UTF-8 string.
public static string ConvertFromBytesToUtf8(List<byte> list)
Parameters
Returns
- string
A UTF-8 decoded string from the byte array.
ConvertFromUtf8ToBytes(string)
Converts a UTF-8 string to a list of bytes.
public static List<byte> ConvertFromUtf8ToBytes(string text)
Parameters
textstringThe string text to convert.
Returns
EqualDateWithoutTime(DateTime, DateTime)
Compares two DateTime values to check if they represent the same date, ignoring time components.
public static bool EqualDateWithoutTime(DateTime firstDateTime, DateTime secondDateTime)
Parameters
firstDateTimeDateTimeThe first DateTime to compare.
secondDateTimeDateTimeThe second DateTime to compare.
Returns
- bool
True if both dates have the same day, month, and year; otherwise, false.
FalseOrNull(object)
Determines whether the specified object value is null or represents a false boolean value.
public static bool FalseOrNull(object value)
Parameters
valueobjectThe object value to check.
Returns
- bool
True if the value is null or equals the string representation of false; otherwise, false.
FromHex(string)
Converts hexadecimal string to integer.
public static int FromHex(string hexText)
Parameters
hexTextstringHexadecimal string value.
Returns
- int
Converted integer value.
GetMaxValueForType(Type)
Gets the maximum value for a given numeric type.
public static object GetMaxValueForType(Type type)
Parameters
typeTypeThe numeric type to get max value for.
Returns
- object
Maximum value for the type.
Exceptions
- Exception
Thrown when type is not a numeric type.
GetMinValueForType(Type)
Gets the minimum value for the specified numeric type.
public static object GetMinValueForType(Type type)
Parameters
typeTypeThe numeric type to get the minimum value for.
Returns
- object
The minimum value for the specified type.
Exceptions
- Exception
Thrown when the type is not a supported numeric type.
GetNumberedListFromTo(int, int)
Generates a numbered list of strings from a starting value to a maximum value (inclusive).
public static string[] GetNumberedListFromTo(int start, int max)
Parameters
Returns
- string[]
An array of strings representing numbers from the start to max value.
GetNumberedListFromTo(int, int, string)
Generates a numbered list of strings with a custom postfix from a starting value to a maximum value.
public static List<string> GetNumberedListFromTo(int start, int max, string postfix = ". ")
Parameters
startintThe starting number.
maxintThe count of numbers to generate.
postfixstringThe postfix to append to each number (default is ". ").
Returns
GetOnlyNonNullValues(params string[])
Returns only non-null key-value pairs from the arguments. Arguments are expected in alternating key-value order.
public static List<string> GetOnlyNonNullValues(params string[] args)
Parameters
argsstring[]Array of string arguments in key-value pairs.
Returns
GetValueOfNullable(bool?)
Returns the boolean value from a nullable boolean. If the value is null, returns false.
public static bool GetValueOfNullable(bool? value)
Parameters
valuebool?The nullable boolean value to extract.
Returns
- bool
The boolean value if it has one; otherwise, false.
IntToBool(int)
Converts an integer to a boolean value. 0 returns false, all other values return true.
public static bool IntToBool(int value)
Parameters
valueintThe integer value to convert.
Returns
- bool
True if the value is non-zero; otherwise, false.
IntToBool(object)
Converts an object value to a boolean. 0 returns false, all other integer values return true. Empty string returns false.
public static bool IntToBool(object value)
Parameters
valueobjectThe object value to convert.
Returns
- bool
True if the value represents a non-zero integer; otherwise, false.
Invert(bool, bool)
Inverts a boolean value conditionally based on the isInverting flag.
public static bool Invert(bool value, bool isInverting)
Parameters
valueboolThe boolean value to potentially invert.
isInvertingboolIf true, the value will be inverted; otherwise, returns the original value.
Returns
- bool
The inverted or original boolean value based on the isInverting flag.
Is(bool, bool)
Returns the value, optionally negated.
public static bool Is(bool value, bool isNegated)
Parameters
Returns
- bool
Original or negated value.
IsAllEquals(bool, params bool[])
Checks whether all elements in the array have the same value as the first parameter.
public static bool IsAllEquals(bool value, params bool[] values)
Parameters
Returns
- bool
True if all values match, false otherwise.
IsBool(string)
Determines whether the specified string value can be parsed as a boolean.
public static bool IsBool(string text)
Parameters
textstringThe string value to validate.
Returns
- bool
True if the value can be parsed as boolean; otherwise, false.
IsByte(string)
Determines whether the specified string value can be parsed as a byte.
public static bool IsByte(string text)
Parameters
textstringThe string value to validate.
Returns
- bool
True if the value can be parsed as byte; otherwise, false.
IsByte(string, out byte)
Determines whether the specified string value can be parsed as a byte and outputs the result.
public static bool IsByte(string text, out byte result)
Parameters
textstringThe string value to validate.
resultbyteWhen this method returns, contains the parsed byte value if successful, or 0 if parsing failed.
Returns
- bool
True if the value can be parsed as byte; otherwise, false.
IsDateTime(string)
Determines whether the specified string value can be parsed as a DateTime.
public static bool IsDateTime(string text)
Parameters
textstringThe string value to validate.
Returns
- bool
True if the value can be parsed as DateTime; otherwise, false.
IsDouble(string, bool)
Checks if the string value can be parsed as a double number.
public static bool IsDouble(string text, bool isReplacingCommaForDot = false)
Parameters
textstringThe string value to check.
isReplacingCommaForDotboolWhether to replace comma with dot before parsing.
Returns
- bool
True if value can be parsed as double, false otherwise.
IsFloat(string, bool)
Checks if the string value can be parsed as a float number.
public static bool IsFloat(string text, bool isReplacingCommaForDot = false)
Parameters
textstringThe string value to check.
isReplacingCommaForDotboolWhether to replace comma with dot before parsing.
Returns
- bool
True if value can be parsed as float, false otherwise.
IsInRange(int, int, int)
Checks if the value is within the specified range (inclusive).
public static bool IsInRange(int from, int to, int value)
Parameters
Returns
- bool
True if value is in range, false otherwise.
IsInt(string, bool, bool)
Checks if the string value can be parsed as an integer. Usage: Exceptions.IsInt.
public static bool IsInt(string text, bool isThrowingExceptionIfFloat = false, bool isReplacingCommaForDot = false)
Parameters
textstringThe string value to check.
isThrowingExceptionIfFloatboolWhether to throw an exception if the value is a float number.
isReplacingCommaForDotboolWhether to replace comma with dot before parsing.
Returns
- bool
True if value can be parsed as int, false otherwise.
Exceptions
- Exception
Thrown when value is float and isThrowingExceptionIfFloat is true.
IsLong(string, bool, bool)
Checks if the string value can be parsed as a long number.
public static bool IsLong(string text, bool isThrowingExceptionIfDouble = false, bool isReplacingCommaForDot = false)
Parameters
textstringThe string value to check.
isThrowingExceptionIfDoubleboolWhether to throw exception if value is a double number.
isReplacingCommaForDotboolWhether to replace comma with dot before parsing.
Returns
- bool
True if value can be parsed as long, false otherwise.
Exceptions
- Exception
Thrown when value is double and isThrowingExceptionIfDouble is true.
MakeUpTo2NumbersToZero(int)
Pads a number with a leading zero to ensure at least 2 digits.
public static object MakeUpTo2NumbersToZero(int number)
Parameters
numberintThe number to pad.
Returns
- object
A string representation with a leading zero if the number has only 1 digit.
MakeUpTo3NumbersToZero(int)
Pads a number with leading zeros to ensure at least 3 digits.
public static object MakeUpTo3NumbersToZero(int number)
Parameters
numberintThe number to pad.
Returns
- object
A string representation with leading zeros if the number has less than 3 digits.
MethodForParse<T1>()
Returns a Func delegate that can parse a string to the specified type T1, or null if the type is not supported.
public static object? MethodForParse<T1>()
Returns
- object
A Func delegate for parsing to type T1, or null if the type is not supported.
Type Parameters
T1The target type for parsing.
ParseBool(string)
Parses a string value to a boolean. Returns false if parsing fails.
public static bool ParseBool(string text)
Parameters
textstringThe string value to parse.
Returns
- bool
The parsed boolean value, or false if parsing fails.
ParseBool(string, bool)
Parses a string value to a boolean. Returns the default value if parsing fails.
public static bool ParseBool(string text, bool defaultValue)
Parameters
textstringThe string value to parse.
defaultValueboolThe default value to return if parsing fails.
Returns
- bool
Parsed boolean value or default value if parsing fails.
ParseByte(string)
Parses the specified string value to a byte. Returns byte.MinValue if parsing fails.
public static byte ParseByte(string text)
Parameters
textstringThe string value to parse.
Returns
- byte
The parsed byte value or byte.MinValue if parsing fails.
ParseByte(string, byte)
Parses a string value to a byte with a default value.
public static byte ParseByte(string text, byte defaultValue)
Parameters
textstringThe string value to parse.
defaultValuebyteThe default value to return if parsing fails.
Returns
- byte
The parsed byte value or the specified default value if parsing fails.
ParseDouble(string, double)
Parses a string value to a double, removing spaces before parsing.
public static double ParseDouble(string text, double defaultValue)
Parameters
textstringThe string value to parse.
defaultValuedoubleThe default value to return if parsing fails.
Returns
- double
The parsed double value or the specified default value if parsing fails.
ParseFloat(string)
Parses a string value to a float, replacing comma with dot for decimal separation.
public static float ParseFloat(string text)
Parameters
textstringThe string value to parse.
Returns
- float
The parsed float value or float.MinValue if parsing fails.
ParseInt(string)
Parses a string value to an integer, removing spaces before parsing. If the value cannot be parsed, returns int.MinValue.
public static int ParseInt(string text)
Parameters
textstringThe string value to parse.
Returns
- int
The parsed integer value, or int.MinValue if parsing fails.
ParseInt(string, bool)
Parses a string value to an int with optional strict parsing requirement.
public static int ParseInt(string text, bool isRequiringAllNumbers)
Parameters
textstringThe string value to parse.
isRequiringAllNumbersboolIf true, returns int.MinValue when parsing fails; otherwise, returns 0.
Returns
- int
The parsed int value, int.MinValue if strict mode fails, or 0 if non-strict mode fails.
ParseInt(string, int)
Parses a string value to an int, removing spaces before parsing.
public static int ParseInt(string text, int defaultValue)
Parameters
textstringThe string value to parse.
defaultValueintThe default value to return if parsing fails.
Returns
- int
The parsed int value or the specified default value if parsing fails.
ParseInt(string, int?)
Parses the specified string value to a nullable int with a default value.
public static int? ParseInt(string text, int? defaultValue)
Parameters
textstringThe string value to parse.
defaultValueint?The default nullable value to return if parsing fails.
Returns
- int?
The parsed int value or the specified default value if parsing fails.
ParseIntNull(string)
Parses string to nullable int, returns null if parsing fails.
public static int? ParseIntNull(string text)
Parameters
textstringThe string value to parse.
Returns
- int?
Parsed integer or null if parsing failed.
ParseShort(string)
Parses the specified string value to a short. Returns short.MinValue if parsing fails.
public static short ParseShort(string text)
Parameters
textstringThe string value to parse.
Returns
- short
The parsed short value or short.MinValue if parsing fails.
ParseShort(string, short)
Parses the specified string value to a short with a default value.
public static short ParseShort(string text, short defaultValue)
Parameters
textstringThe string value to parse.
defaultValueshortThe default value to return if parsing fails.
Returns
- short
The parsed short value or the specified default value if parsing fails.
RemoveNotNumber(IList)
Removes elements from the list that are not valid numbers. Modifies the list directly.
public static void RemoveNotNumber(IList list)
Parameters
listIListThe list to filter, removing non-numeric elements.
Replace(ref string, bool)
Replaces comma with dot in the string value if specified.
public static string Replace(ref string text, bool isReplacingCommaForDot)
Parameters
textstringThe string value to modify.
isReplacingCommaForDotboolWhether to replace comma with dot.
Returns
- string
Modified string value.
SameLengthAllDateTimes(DateTime)
Formats a DateTime to a fixed-length date-time string (dd.MM.yyyy HH:mm:ss format). Does not shorten the year, uses standard 4-digit format.
public static string SameLengthAllDateTimes(DateTime dateTime)
Parameters
dateTimeDateTimeThe DateTime to format.
Returns
- string
A formatted date-time string in dd.MM.yyyy HH:mm:ss format.
SameLengthAllDates(DateTime)
Formats a DateTime to a date string with consistent padding (dd.MM.yyyy format).
public static string SameLengthAllDates(DateTime dateTime)
Parameters
dateTimeDateTimeThe DateTime to format.
Returns
- string
A formatted date string in dd.MM.yyyy format.
SameLengthAllTimes(DateTime)
Formats a DateTime to a time string with consistent padding (HH:mm:ss format).
public static string SameLengthAllTimes(DateTime dateTime)
Parameters
dateTimeDateTimeThe DateTime to format.
Returns
- string
A formatted time string in HH:mm:ss format.
StreamFromString(string)
Creates a Stream from a string.
public static Stream StreamFromString(string text)
Parameters
textstringThe text to convert to stream.
Returns
- Stream
MemoryStream containing the text.
StringFromStream(Stream)
Reads all text from a Stream.
public static string StringFromStream(Stream stream)
Parameters
streamStreamThe stream to read from.
Returns
- string
String containing all text from the stream.
StringToBool(string)
Returns bool representation of the text value. Returns true for "Yes", "True", "1", or "Ano" (Czech Yes).
public static bool StringToBool(string text)
Parameters
textstringThe string value to convert to boolean.
Returns
- bool
True if the text matches a known truthy value; otherwise, false.
ToString<T>(T)
Converts value to string using ToString method.
public static string ToString<T>(T value) where T : notnull
Parameters
valueTThe value to convert.
Returns
- string
String representation of the value.
Type Parameters
TType of the value.
TryParseBool(string)
Tries to parse the string value as a boolean.
public static bool TryParseBool(string text)
Parameters
textstringThe string value to parse.
Returns
- bool
True if parsing succeeded, false otherwise.
TryParseBool(string, bool)
Attempts to parse a string value to a boolean with a default value. Returns the parsed value if parsing succeeds, otherwise returns the default value.
public static bool TryParseBool(string text, bool defaultValue)
Parameters
textstringThe string value to parse.
defaultValueboolThe default value to return if parsing fails.
Returns
- bool
The parsed boolean value or the specified default value if parsing fails.
TryParseByte(string, byte)
Attempts to parse a string value to a byte with a default value.
public static byte TryParseByte(string text, byte defaultValue)
Parameters
textstringThe string value to parse.
defaultValuebyteThe default value to return if parsing fails.
Returns
- byte
The parsed byte value or the specified default value if parsing fails.
TryParseDateTime(string)
Attempts to parse a string value to a DateTime and stores the result in LastDateTime.
public static bool TryParseDateTime(string text)
Parameters
textstringThe string value to parse.
Returns
- bool
True if parsing succeeded; otherwise, false.
TryParseDateTime(string, CultureInfo, DateTime)
Parses a string value to a DateTime using the specified culture info. For parsing from serialized file use DTHelperEn.
public static DateTime TryParseDateTime(string text, CultureInfo cultureInfo, DateTime defaultValue)
Parameters
textstringThe string value to parse.
cultureInfoCultureInfoThe culture info to use for parsing.
defaultValueDateTimeThe default value to return if parsing fails.
Returns
- DateTime
The parsed DateTime value or the specified default value if parsing fails.
TryParseInt(string, int)
Attempts to parse a string value to an int with a default value.
public static int TryParseInt(string text, int defaultValue)
Parameters
textstringThe string value to parse.
defaultValueintThe default value to return if parsing fails.
Returns
- int
The parsed int value or the specified default value if parsing fails.
TryParseInt(string, int, bool)
Attempts to parse a string value to an int with optional exception throwing.
public static int TryParseInt(string text, int defaultValue, bool isThrowingException)
Parameters
textstringThe string value to parse.
defaultValueintThe default value to return if parsing fails.
isThrowingExceptionboolIf true, throws an exception when parsing fails; otherwise, returns the default value.
Returns
- int
The parsed int value or the specified default value if parsing fails.
TryParseIntCheckNull(string, int)
Attempts to parse a string value to an int with null checking, returns 0 if value is null.
public static int TryParseIntCheckNull(string text, int defaultValue)
Parameters
textstringThe string value to parse.
defaultValueintThe default value to return if parsing fails.
Returns
- int
0 if value is null, the parsed int value if successful, or the default value if parsing fails.
TryParseUint(string)
Attempts to parse a string value to a uint and stores the result in LastUint.
public static bool TryParseUint(string text)
Parameters
textstringThe string value to parse.
Returns
- bool
True if parsing succeeded; otherwise, false.
UsaDateTimeToString(DateTime)
Formats a DateTime to a US-style date-time string (M/d/yyyy H:m:s format).
public static string UsaDateTimeToString(DateTime dateTime)
Parameters
dateTimeDateTimeThe DateTime to format.
Returns
- string
A formatted date-time string in US format (M/d/yyyy H:m:s).