Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert KB to MB

Status
Not open for further replies.
Jun 9, 2006
159
US
Hello,

How can I use C# to calculate a conversion from a value in kilobytes to Megabytes?

-- shawn

Shawn Molloy
Seattle, WA
 
Nevermind - that was a stupid question!

Shawn Molloy
Seattle, WA
 
Just as an aside, they have included KB, MB,and GB into Powershell as constants. So:

16:12:24 C:\>1GB
1073741824
16:12:30 C:\>1MB
1048576
16:12:32 C:\>1KB
1024
16:12:37 C:\>1GB / 1KB
1048576
16:12:51 C:\>

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Can you access the Powershell from the .NET framework? Is there some constants you have access to in C# to more easily perform this operation?



Shawn Molloy
Seattle, WA
 
I'm sorry but why would you access an external component to perform such simple calculations?

want something "complex"? Something like this should work.




public enum SizeDefinition
{
BYTE = 1, KILOBYTE = 2, MEGABYTE = 3, GIGABYTE = 4
}

public double ConvertAmount(int amount, SizeDefinition from, SizeDefinition to)
{
int difference = 0;

if ((int)from > (int)to)
{
difference = (int)from - (int)to;
return (double)((double)amount) * (1024d * (double)difference);
}
else
{
difference = (int)to - (int)from;
return (double)((double)amount) / (1024d * (double)difference);
}
}




public void Convert()
{
int amt = ConvertAmount(5, SizeDefinition.MEGABYTE, SizeDefinition.KILOBYTE);

MessageBox.Show("5 MB = " + amt + " KB");
}
 
Keep in mind if you pass in the same value you may get a divide by zero error.

I didn't test that code or anything...
 
All you have to do is set a property and read another. For example you set the MegaBytes property to 896 and then read the GigaBytes in order to convert to GB or the Bytes property in order to convert to bytes (in our case that would be 0.875 GB or 939524096 B )

public class Converter
{
public static readonly Int64 KILOBYTE = 1024;
public static readonly Int64 MEGABYTE = 1024 * KILOBYTE;
public static readonly Int64 GIGABYTE = 1024 * MEGABYTE;
public static readonly Int64 TERABYTE = 1024 * GIGABYTE;

private Int64 m_i64Size = 0;

public Converter( Int64 iInitialSize )
{
m_i64Size = iInitialSize;
}//ctor


public Int64 Bytes
{
get
{
return m_i64Size;
}//get
set
{
if( value < 0 )
{
throw new ArgumentException( "Positive only, please." );
}//if

m_i64Size = value;
}
}//prop

public Double KiloBytes
{
get
{
return (double)(m_i64Size) / KILOBYTE;
}
set
{
if( value < 0 )
{
throw new ArgumentException( "Positive only, please." );
}//if
m_i64Size = (Int64)(value * KILOBYTE);
}
}//prop


public Double MegaBytes
{
get
{
return (double)m_i64Size / MEGABYTE;
}
set
{
if( value < 0 )
{
throw new ArgumentException( "Positive only, please." );
}//if

m_i64Size = (Int64)(value * MEGABYTE);
}
}//prop

public Double GigaBytes
{
get
{
return (double)m_i64Size / GIGABYTE;
}
set
{
if( value < 0 )
{
throw new ArgumentException( "Positive only, please." );
}//if
m_i64Size = (Int64)(value * GIGABYTE);
}
}//prop
}//class_Converter
 
I would point out that my aside above was just that. An aside. I did not intend it in anyway as a solution to the expressed problem. It was just something that the problem reminded me of. That is why I started the post with "Just as an aside...".

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top