 # Some Common Conversion Functions – Part I

Posted by: Suprotim Agarwal , on 8/14/2008, in Category WinForms & WinRT
Views: 45557
Abstract: In this article, we will explore some commonly used conversion functions in our projects. This is the first part of our two part series. I will cover up some other commonly used conversions in the second part.
Some Common Conversion Functions – Part I

In this article, we will explore some commonly used conversion functions in our projects. This is the first part of our two part series. I will cover up some other commonly used conversions in the second part. If you feel, there are any other conversion functions to be covered, use the Contact page to drop me a mail.
1 degree = 0.0174532925 radian. To convert a value in degrees to radians, multiply it by (pi/180). .NET contains the static field Math.PI which represents π (pi). To convert degree to radian in .NET, use the code below:

C#
{
return (Math.PI / 180) * degree;
}

VB.NET
Public Shared Function DegreeToRadian(ByVal degree As Double) As Double
Return (Math.PI / 180) * degree
End Function

1 radian = 57.2957795 degree. To convert a value in radians to degree, multiply it by (180/pi). To convert radian to degree in .NET, use the code below:

C#
{
return (180 / Math.PI) * radian;
}
VB.NET
Return (180 / Math.PI) * radian
End Function

Round Float to Integer
The Math.Round() returns the whole number nearest the specified value.
C#
// Round Float to Integer
public static int RoundFloatToInt(float f)
{
return ((int)Math.Round(f));

// If you want to round to a fixed decimal place, say 3
// return ((int)Math.Round(f,3));
}
VB.NET
' Round Float to Integer
Public Shared Function RoundFloatToInt(ByVal f As Single) As Integer
Return (CInt(Fix(Math.Round(f))))

' If you want to round to a fixed decimal place, say 3
' Return (CInt(Fix(Math.Round(f,3));
End Function
If you want to round a number to a fixed decimal place, specify the number in the Round method as shown below:
public static double Round(double, int);
For eg: If you want to round to a fixed decimal place, say 3, use :
return ((int)Math.Round(f,3));

Convert Celsius To Fahrenheit
1 degree Celsius = 33.8 degrees Fahrenheit.
Formula is f =   (9 /5 * c) + 32. Here’s how it is implemented in .NET
C#
// Celcius to Fahrenheit
public static double CelciusToFahrenheit(double cel)
{
return (((0.9 / 0.5) * cel) + 32);
}
VB.NET
' Celsius to Fahrenheit
Public Shared Function CelsiusToFahrenheit(ByVal cel As Double) As Double
Return (((0.9 / 0.5) * cel) + 32)
End Function

Convert Fahrenheit to Celsius
32 degree Fahrenheit = 0 degree Celsius
Formula is c = 5/9 * (f-32). Here’s how it is implemented in .NET
C#
// Fahrenheit to Celsius
public static double FahrenheitToCelsius(double farh)
{
return ((0.5 / 0.9) * (farh - 32));
}
VB.NET
' Fahrenheit to Celsius
Public Shared Function FahrenheitToCelsius(ByVal farh As Double) As Double
Return ((0.5 / 0.9) * (farh - 32))
End Function

Convert String To Integer
In order to convert string to integer, use the Int32.Parse(). The Parse method converts the string representation of a number to its 32-bit signed integer equivalent. If the string contains non-numeric values, it throws an error.
C#
// Convert String to Int
public static int StringToInt(string str)
{
return Int32.Parse(str);
}
VB.NET
' Convert String to Int
Public Shared Function StringToInt(ByVal str As String) As Integer
Return Int32.Parse(str)
End Function

Convert String to Base64 string
You will have to use the methods in System.Text.Encoding to convert string to Base64. The conversion involves two processes:
a.    Convert string to a byte array
b.    Use the Convert.ToBase64String() method to convert the byte array to a Base64 string
C#
// Convert String to Base64
public static string StringToBase64(string str)
{
byte[] b = System.Text.Encoding.UTF8.GetBytes(str);
string b64 = Convert.ToBase64String(b);
return b64;
}
VB.NET
' Convert String to Base64
Public Shared Function StringToBase64(ByVal str As String) As String
Dim b As Byte() = System.Text.Encoding.UTF8.GetBytes(str)
Dim b64 As String = Convert.ToBase64String(b)
Return b64
End Function

Convert Base64 string back to string
In the previous example, we converted a string “I Love dotnet” to Base64 string “SSBsb3ZlIGRvdG5ldA==”. In order to convert a Base64 string back to the original string, use FromBase64String(). The conversion involves two processes:
a.   The FromBase64String() converts the string to a byte array
b.   Use the relevant Encoding method to convert the byte array to a string, in our case UTF8.GetString();

C#
// Convert Base64string to string
public static string Base64ToString(string b64)
{
byte[] b = Convert.FromBase64String(b64);
return(System.Text.Encoding.UTF8.GetString(b));
}
VB.NET
' Convert Base64string to string
Public Shared Function Base64ToString(ByVal b64 As String) As String
Dim b As Byte() = Convert.FromBase64String(b64)
Return(System.Text.Encoding.UTF8.GetString(b))
End Function

Well we covered up some basic conversions functions. There are some more to come in the forthcoming articles. So watch out for them!! You can download the source code of this article from here. I hope this article was useful and I thank you for viewing it.

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Author Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for ten consecutive times. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment. Feedback - Leave us some adulation, criticism and everything in between!
 Comment posted by munira on Saturday, August 16, 2008 4:41 AM
excellent