|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BaseTypes.Utils
{
/// <summary>
/// Converter of numeric objects
/// </summary>
public static class Converter
{
/// <summary>
/// Converts object to byte
/// </summary>
/// <param name="o">The object</param>
/// <returns>Conversion resule</returns>
public static byte ToByte(object o)
{
if (o is Double)
{
double b = (double)o;
return (byte)b;
}
if (o is Byte)
{
byte b = (byte)o;
return b;
}
if (o is UInt16)
{
ushort b = (ushort)o;
return (byte)b;
}
if (o is UInt32)
{
uint b = (uint)o;
return (byte)b;
}
if (o is UInt64)
{
ulong b = (ulong)o;
return (byte)b;
}
if (o is SByte)
{
sbyte b = (sbyte)o;
return (byte)b;
}
if (o is Int16)
{
short b = (short)o;
return (byte)b;
}
if (o is Int32)
{
int b = (int)o;
return (byte)b;
}
if (o is Int64)
{
long b = (long)o;
return (byte)b;
}
return 0;
}
/// <summary>
/// Converts object to ushort
/// </summary>
/// <param name="o">The object</param>
/// <returns>Conversion result</returns>
public static ushort ToUInt16(object o)
{
if (o is Double)
{
double b = (double)o;
return (ushort)b;
}
if (o is Byte)
{
byte b = (byte)o;
return (ushort)b;
}
if (o is UInt16)
{
ushort b = (ushort)o;
return b;
}
if (o is UInt32)
{
uint b = (uint)o;
return (ushort)b;
}
if (o is UInt64)
{
ulong b = (ulong)o;
return (ushort)b;
}
if (o is SByte)
{
sbyte b = (sbyte)o;
return (ushort)b;
}
if (o is Int16)
{
short b = (short)o;
return (ushort)b;
}
if (o is Int32)
{
int b = (int)o;
return (ushort)b;
}
if (o is Int64)
{
long b = (long)o;
return (ushort)b;
}
return 0;
}
/// <summary>
/// Converts object to uint
/// </summary>
/// <param name="o">The object</param>
/// <returns>Conversion result</returns>
public static uint ToUInt32(object o)
{
if (o is Double)
{
double b = (double)o;
return (uint)b;
}
if (o is Byte)
{
byte b = (byte)o;
return (uint)b;
}
if (o is UInt16)
{
ushort b = (ushort)o;
return (uint)b;
}
if (o is UInt32)
{
uint b = (uint)o;
return b;
}
if (o is UInt64)
{
ulong b = (ulong)o;
return (uint)b;
}
if (o is SByte)
{
sbyte b = (sbyte)o;
return (uint)b;
}
if (o is Int16)
{
short b = (short)o;
return (uint)b;
}
if (o is Int32)
{
int b = (int)o;
return (uint)b;
}
if (o is Int64)
{
long b = (long)o;
return (uint)b;
}
return 0;
}
/// <summary>
/// Converts object to ulong
/// </summary>
/// <param name="o">The object</param>
/// <returns>Conversion result</returns>
public static ulong ToUInt64(object o)
{
if (o is Double)
{
double b = (double)o;
return (ulong)b;
}
if (o is Byte)
{
byte b = (byte)o;
return (ulong)b;
}
if (o is UInt16)
{
ushort b = (ushort)o;
return (ulong)b;
}
if (o is UInt32)
{
uint b = (uint)o;
return (ulong)b;
}
if (o is UInt64)
{
ulong b = (ulong)o;
return b;
}
if (o is SByte)
{
sbyte b = (sbyte)o;
return (ulong)b;
}
if (o is Int16)
{
short b = (short)o;
return (ulong)b;
}
if (o is Int32)
{
int b = (int)o;
return (ulong)b;
}
if (o is Int64)
{
long b = (long)o;
return (ulong)b;
}
return 0;
}
/// <summary>
/// Converts object to sbyte
/// </summary>
/// <param name="o">The object</param>
/// <returns>Conversion result</returns>
public static sbyte ToSByte(object o)
{
if (o is Double)
{
double b = (double)o;
return (sbyte)b;
}
if (o is Byte)
{
byte b = (byte)o;
return (sbyte)b;
}
if (o is UInt16)
{
ushort b = (ushort)o;
return (sbyte)b;
}
if (o is UInt32)
{
uint b = (uint)o;
return (sbyte)b;
}
if (o is UInt64)
{
ulong b = (ulong)o;
return (sbyte)b;
}
if (o is SByte)
{
sbyte b = (sbyte)o;
return b;
}
if (o is Int16)
{
short b = (short)o;
return (sbyte)b;
}
if (o is Int32)
{
int b = (int)o;
return (sbyte)b;
}
if (o is Int64)
{
long b = (long)o;
return (sbyte)b;
}
return 0;
}
/// <summary>
/// Converts object to short
/// </summary>
/// <param name="o">The object</param>
/// <returns>Conversion result</returns>
public static short ToInt16(object o)
{
if (o is Double)
{
double b = (double)o;
return (short)b;
}
if (o is Byte)
{
byte b = (byte)o;
return (short)b;
}
if (o is UInt16)
{
ushort b = (ushort)o;
return (short)b;
}
if (o is UInt32)
{
uint b = (uint)o;
return (short)b;
}
if (o is UInt64)
{
ulong b = (ulong)o;
return (short)b;
}
if (o is SByte)
{
sbyte b = (sbyte)o;
return (short)b;
}
if (o is Int16)
{
short b = (short)o;
return b;
}
if (o is Int32)
{
int b = (int)o;
return (short)b;
}
if (o is Int64)
{
long b = (long)o;
return (short)b;
}
return 0;
}
/// <summary>
/// Converts object to int
/// </summary>
/// <param name="o">The object</param>
/// <returns>Conversion result</returns>
public static int ToInt32(object o)
{
if (o is Double)
{
double b = (double)o;
return (int)b;
}
if (o is Byte)
{
byte b = (byte)o;
return (int)b;
}
if (o is UInt16)
{
ushort b = (ushort)o;
return (int)b;
}
if (o is UInt32)
{
uint b = (uint)o;
return (int)b;
}
if (o is UInt64)
{
ulong b = (ulong)o;
return (int)b;
}
if (o is SByte)
{
sbyte b = (sbyte)o;
return (int)b;
}
if (o is Int16)
{
short b = (short)o;
return (int)b;
}
if (o is Int32)
{
int b = (int)o;
return b;
}
if (o is Int64)
{
long b = (long)o;
return (int)b;
}
return 0;
}
/// <summary>
/// Converts object to long
/// </summary>
/// <param name="o">The object</param>
/// <returns>Conversion result</returns>
public static long ToInt64(object o)
{
if (o is Double)
{
double b = (double)o;
return (long)b;
}
if (o is Byte)
{
byte b = (byte)o;
return (long)b;
}
if (o is UInt16)
{
ushort b = (ushort)o;
return (long)b;
}
if (o is UInt32)
{
uint b = (uint)o;
return (long)b;
}
if (o is UInt64)
{
ulong b = (ulong)o;
return (long)b;
}
if (o is SByte)
{
sbyte b = (sbyte)o;
return (long)b;
}
if (o is Int16)
{
short b = (short)o;
return (long)b;
}
if (o is Int32)
{
int b = (int)o;
return (long)b;
}
if (o is Int64)
{
long b = (long)o;
return b;
}
return 0;
}
/// <summary>
/// Converts object to double
/// </summary>
/// <param name="o">The object</param>
/// <returns>Conversion result</returns>
public static double ToDouble(object o)
{
if (o is decimal)
{
decimal dec = (decimal)o;
return Double.Parse(dec + "");
}
if (o is Double)
{
double b = (double)o;
return b;
}
if (o is Byte)
{
byte b = (byte)o;
return (double)b;
}
if (o is UInt16)
{
ushort b = (ushort)o;
return (double)b;
}
if (o is UInt32)
{
uint b = (uint)o;
return (double)b;
}
if (o is UInt64)
{
ulong b = (ulong)o;
return (double)b;
}
if (o is SByte)
{
sbyte b = (sbyte)o;
return (double)b;
}
if (o is Int16)
{
short b = (short)o;
return (double)b;
}
if (o is Int32)
{
int b = (int)o;
return (double)b;
}
if (o is Int64)
{
long b = (long)o;
return (double)b;
}
if (o is Boolean)
{
bool b = (bool)o;
return b ? 1 : 0;
}
return 0;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Ph. D. Petr Ivankov worked as scientific researcher at Russian Mission Control Centre since 1978 up to 2000. Now he is engaged by Aviation training simulators http://dinamika-avia.com/ . His additional interests are:
1) Noncommutative geometry
http://front.math.ucdavis.edu/author/P.Ivankov
2) Literary work (Russian only)
http://zhurnal.lib.ru/editors/3/3d_m/
3) Scientific articles
http://arxiv.org/find/all/1/au:+Ivankov_Petr/0/1/0/all/0/1