Thursday 29 July, 2010

Some Common Conversion Functions

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.
Convert Degree To Radian
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#
// Convert Degree to Radian
public static double DegreeToRadian(double degree)
{
return (Math.PI / 180) * degree;
}
VB.NET
' Convert Degree to Radian
Public Shared Function DegreeToRadian(ByVal degree As Double) As Double
Return (Math.PI / 180) * degree
End Function
Convert Radian To Degree
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#
// Convert Radian To Degree
public static double RadianToDegree(double radian)
{
return (180 / Math.PI) * radian;
}
VB.NET
' Convert Radian To Degree
Public Shared Function RadianToDegree(ByVal radian As Double) As Double
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

No comments:

Post a Comment