Daily Archives: May 14, 2021

C# || How To Convert Bytes To Kilobytes, Megabytes, Gigabytes, Terabytes Using C#

The following is a module with functions which demonstrates how to convert bytes to decimal formats like kilobytes, megabytes, gigabytes, terabytes, petabytes, exabytes, zettabytes, and yottabytes, as well as binary formats like kibibytes, mebibytes, gibibytes, tebibytes, pebibytes, exbibytes, zebibytes, and yobibytes using C#.

The function demonstrated on this page follows the IEC standard, which means:


• 1 kilobyte = 1000 bytes (Decimal)
• 1 kibibyte = 1024 bytes (Binary)

This function allows you to convert bytes to a measurement unit, a measurement unit to bytes, and allows to convert from one measurement unit to another measurement unit.


1. Convert Bytes To Measurement Unit

The example below demonstrates the use of ‘Utils.Bytes.FromTo‘ to convert bytes to a measurement unit.

The optional function parameter allows you to specify the decimal places.


2. Convert Measurement Unit To Bytes

The example below demonstrates the use of ‘Utils.Bytes.FromTo‘ to convert a measurement unit to bytes.

The optional function parameter allows you to specify the decimal places.


3. Convert Measurement Unit To Measurement Unit

The example below demonstrates the use of ‘Utils.Bytes.FromTo‘ to convert a measurement unit to another measurement unit.

The optional function parameter allows you to specify the decimal places.


4. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


5. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Parse A Delimited CSV File Using C#

The following is a module with functions which demonstrates how to parse a delimited CSV file using VB.NET.

This function parses a CSV file and returns its results as a List. Each List index represents a line in the CSV file, with each item in the list representing a record contained on that line.

The function demonstrated on this page uses FileIO.TextFieldParser to parse values in a CSV file.

Note: To use the function in this module, make sure you have a reference to ‘Microsoft.VisualBasic‘ in your project.

One way to do this is, in your Solution Explorer (where all the files are shown with your project), right click the ‘References‘ folder, click ‘Add Reference‘, then type ‘Microsoft.VisualBasic‘ in the search box, and add the reference titled Microsoft.VisualBasic in the results Tab.


1. Parse CSV File

The example below demonstrates the use of ‘Utils.Methods.ParseCsv‘ to parse a CSV file and return its results as a List.

The optional function parameter allows you to specify the delimiters. Default delimiter is a comma (,).

Sample CSV used in this example is the following:


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Round A Number To The Nearest X Using C#

The following is a module with functions which demonstrates how to round a number to the nearest X using C#.

This function has the ability to either round a number to the nearest amount, always round up, or always round down. For example, when dealing with money, this is good for rounding a dollar amount to the nearest 5 cents.


1. Round – Nearest

The example below demonstrates the use of ‘Utils.Round.Amount‘ to round a number to the nearest 5 cents.

The optional function parameter determines they type of rounding to perform.


2. Round – Up

The example below demonstrates the use of ‘Utils.Round.Amount‘ to always round a number up to the nearest 5 cents.

The optional function parameter determines they type of rounding to perform.


3. Round – Down

The example below demonstrates the use of ‘Utils.Round.Amount‘ to always round a number down to the nearest 5 cents.

The optional function parameter determines they type of rounding to perform.


4. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


5. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Replace A Letter With Its Alphabet Position Using C#

The following is a module with functions which demonstrates how to replace a letter with its alphabet position using C#.


1. Replace With Alphabet Position

The example below demonstrates the use of ‘Utils.Methods.GetAlphabetPosition‘ to replace a letter with its alphabet position.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C# || How To Generate A Random String Of A Specified Length Using C#

The following is a module with functions which demonstrates how to generate a random code of a specified length using C#.

The function demonstrated on this page has the ability to generate random strings that contains only letters, only numerical digits, or alphanumeric strings.


1. Random Code – Alphabetical

The example below demonstrates the use of ‘Utils.Code.GetRandom‘ to generate a code of a specified length that contains only letters.

The optional function parameter determines the type of code that is generated.


2. Random Code – Numeric

The example below demonstrates the use of ‘Utils.Code.GetRandom‘ to generate a code of a specified length that contains only digits.

The optional function parameter determines the type of code that is generated.


3. Random Code – Alphanumeric

The example below demonstrates the use of ‘Utils.Code.GetRandom‘ to generate a code of a specified length that is alphanumeric.

The optional function parameter determines the type of code that is generated.


4. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


5. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.