Daily Archives: May 8, 2021

C# || How To Convert A String To Byte Array & Byte Array To String Using C#

The following is a module with functions which demonstrates how to convert a string to a byte array and a byte array to a string using C#.


1. String To Byte Array

The example below demonstrates the use of ‘Utils.Extensions.GetBytes‘ to convert a string to a byte array. The optional parameter allows to specify the encoding.


2. Byte Array To String

The example below demonstrates the use of ‘Utils.Extensions.GetString‘ to convert a byte array to a string. The optional parameter allows to specify the encoding.


3. Utils Namespace

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


4. 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 Save, Open & Read File As A Byte Array & Memory Stream Using C#

The following is a module with functions which demonstrates how to save, open and read a file as a byte array and memory stream using C#.


1. Read File – Byte Array

The example below demonstrates the use of ‘Utils.Methods.ReadFile‘ to read a file as a byte array.


2. Read File – Memory Stream

The example below demonstrates the use of ‘Utils.Methods.ReadFile‘ to read a file as a memory stream.


3. Save File – Byte Array

The example below demonstrates the use of ‘Utils.Methods.SaveFile‘ to save the contents of a byte array to a file.


4. Save File – Memory Stream

The example below demonstrates the use of ‘Utils.Methods.SaveFile‘ to save the contents of a memory stream to a file.


5. Utils Namespace

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


6. 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 Validate An Email Address Using C#

The following is a module with functions which demonstrates how to determine if an email address is valid using C#.

This function is based on RFC2821 and RFC2822 to determine the validity of an email address.


1. Validate Email – Basic Usage

The example below demonstrates the use of ‘Utils.Email.IsValid‘ to determine if an email address is valid.


2. Validate Email – Additional Options

The example below demonstrates the use of ‘Utils.Email.IsValid‘ to determine if an email address is valid with additional validation options.

Supplying options to verify an email address uses the default validity rules, in addition to the provided validation options.

This allows you to add custom rules to determine if an email is valid or not. For example, the misspelling of common email domains, or known spam domains.


3. Utils Namespace

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


4. 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 Validate A Phone Number Using C#

The following is a module with functions which demonstrates how to validate a phone number using C#.

Not only does this function check formatting, but follows NANP numbering rules by ensuring that the first digit in the area code and the first digit in the second section are 2-9.


1. Validate Phone Number

The example below demonstrates the use of ‘Utils.Methods.IsValidPhoneNumber‘ to determine if a phone number is valid.


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.