Table of Contents

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

bool

LastByte

Last successfully parsed byte value.

public static byte LastByte { get; set; }

Property Value

byte

LastDateTime

Last successfully parsed DateTime value.

public static DateTime LastDateTime { get; set; }

Property Value

DateTime

LastDouble

Last successfully parsed double value.

public static double LastDouble { get; set; }

Property Value

double

LastFloat

Last successfully parsed float value.

public static float LastFloat { get; set; }

Property Value

float

LastInt

Last successfully parsed int value.

public static int LastInt { get; set; }

Property Value

int

LastLong

Last successfully parsed long value.

public static long LastLong { get; set; }

Property Value

long

LastUint

Stores the last successfully parsed uint value from TryParseUint.

public static uint LastUint { get; set; }

Property Value

uint

Methods

BoolToInt(bool)

Converts a boolean value to an integer (1 for true, 0 for false).

public static int BoolToInt(bool value)

Parameters

value bool

The 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

value bool

The 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

value bool

The boolean value to convert.

isLowerCase bool

If 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

value bool

The boolean value to convert.

isLowerCase bool

If 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

array int[]

The array of integers to convert.

Returns

List<string>

A list of strings representing the string representation of each integer.

CastArrayObjectToString(object[])

Converts an array of objects to a list of strings.

public static List<string> CastArrayObjectToString(object[] array)

Parameters

array object[]

The array of objects to convert.

Returns

List<string>

A list of strings representing the string representation of each object.

CastCollectionIntToShort(List<int>)

Converts a list of integers to a list of shorts.

public static List<short> CastCollectionIntToShort(List<int> list)

Parameters

list List<int>

The list of integers to convert.

Returns

List<short>

A list of shorts converted from the integer list.

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

list List<short>

The list of shorts to convert.

Returns

List<int>

A list of integers converted from the short collection.

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

list IList<string>

The list of strings to convert.

Returns

List<int>

A list of integers parsed from the string collection.

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

list List<short>

The list of shorts to convert.

Returns

List<int>

A list of integers converted from the short list.

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

text string

The string text to cast.

isChar bool

If true, casts the first character; otherwise, casts the entire string.

Returns

T

The casted value of type T.

Type Parameters

T

The 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

list IList<U>

The list to convert.

Returns

List<int>

A list of integers parsed from the source list.

Type Parameters

U

The 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

list List<byte>

The list of bytes to process.

Returns

List<byte>

List of bytes with trailing zeros removed.

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

firstObject object

The first object to compare.

secondObject object

The second object to compare.

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

list List<byte>

The list of bytes to convert.

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

text string

The string text to convert.

Returns

List<byte>

A list of bytes representing the UTF-8 encoded string.

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

firstDateTime DateTime

The first DateTime to compare.

secondDateTime DateTime

The 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

value object

The 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

hexText string

Hexadecimal 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

type Type

The 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

type Type

The 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

start int

The starting number.

max int

The maximum number (inclusive).

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

start int

The starting number.

max int

The count of numbers to generate.

postfix string

The postfix to append to each number (default is ". ").

Returns

List<string>

A list of strings with numbers and the specified postfix.

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

args string[]

Array of string arguments in key-value pairs.

Returns

List<string>

List containing only non-null values.

GetValueOfNullable(bool?)

Returns the boolean value from a nullable boolean. If the value is null, returns false.

public static bool GetValueOfNullable(bool? value)

Parameters

value bool?

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

value int

The 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

value object

The 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

value bool

The boolean value to potentially invert.

isInverting bool

If 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

value bool

The boolean value.

isNegated bool

Whether to negate the value.

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

value bool

The value to compare against.

values bool[]

Array of values to check.

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

text string

The 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

text string

The 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

text string

The string value to validate.

result byte

When 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

text string

The 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

text string

The string value to check.

isReplacingCommaForDot bool

Whether 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

text string

The string value to check.

isReplacingCommaForDot bool

Whether 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

from int

Range start value.

to int

Range end value.

value int

Value to check.

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

text string

The string value to check.

isThrowingExceptionIfFloat bool

Whether to throw an exception if the value is a float number.

isReplacingCommaForDot bool

Whether 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

text string

The string value to check.

isThrowingExceptionIfDouble bool

Whether to throw exception if value is a double number.

isReplacingCommaForDot bool

Whether 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

number int

The 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

number int

The 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

T1

The 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

text string

The 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

text string

The string value to parse.

defaultValue bool

The 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

text string

The 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

text string

The string value to parse.

defaultValue byte

The 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

text string

The string value to parse.

defaultValue double

The 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

text string

The 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

text string

The 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

text string

The string value to parse.

isRequiringAllNumbers bool

If 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

text string

The string value to parse.

defaultValue int

The 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

text string

The string value to parse.

defaultValue int?

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

text string

The 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

text string

The 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

text string

The string value to parse.

defaultValue short

The 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

list IList

The 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

text string

The string value to modify.

isReplacingCommaForDot bool

Whether 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

dateTime DateTime

The 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

dateTime DateTime

The 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

dateTime DateTime

The 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

text string

The 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

stream Stream

The 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

text string

The 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

value T

The value to convert.

Returns

string

String representation of the value.

Type Parameters

T

Type of the value.

TryParseBool(string)

Tries to parse the string value as a boolean.

public static bool TryParseBool(string text)

Parameters

text string

The 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

text string

The string value to parse.

defaultValue bool

The 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

text string

The string value to parse.

defaultValue byte

The 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

text string

The 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

text string

The string value to parse.

cultureInfo CultureInfo

The culture info to use for parsing.

defaultValue DateTime

The 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

text string

The string value to parse.

defaultValue int

The 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

text string

The string value to parse.

defaultValue int

The default value to return if parsing fails.

isThrowingException bool

If 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

text string

The string value to parse.

defaultValue int

The 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

text string

The 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

dateTime DateTime

The DateTime to format.

Returns

string

A formatted date-time string in US format (M/d/yyyy H:m:s).