none
convert byte to KB or MB or GB or TB in C#

    General discussion

  • hi

    i want to convert byte to kB or MB or GB or TB

    ex if input in KB then byte automatically converted in KB

    if input in MB then byte automatically converted in MB

    if input in GB then byte automatically converted in GB

    if input in TB then byte automatically converted in TB

    i want to create only one function will pass only one parameter as byte that function should return converted value accroding to input value(KB, GB, TB) in C#

    please reply me soon its urgent

    thanks in advance 

    Friday, January 13, 2012 5:19 AM

All replies

  • Hi,

    could you tell us, where you have the problem at the moment? Together with the exact specification?

    I do not like to write solutions for others but I am really willing to help you write it on your own.
    (Or in other words: Write your homework alone :) )

    Also important: your question is not really related to Windows Forms. Another forum e.g. the C# General forum might be much better for such a question (in case you use c# of course!)

    With kind regards,

    Konrad

    Friday, January 13, 2012 1:30 PM
  • Most important step first - define what you mean by KB, MB, GB, TB, etc.! 

    Depending on who you ask, you'll need to work with multiples of 1024 or 1000 or both!

     


    Regards, Phill W.
    Friday, January 13, 2012 3:11 PM
  • Most important step first - define what you mean by KB, MB, GB, TB, etc.! 

    Depending on who you ask, you'll need to work with multiples of 1024 or 1000 or both!

     


    Regards, Phill W.


    Phill - KB, MB, GB, etc. would be multiples of 1024, and KiB, MiB, GiB, etc. would be multiples of 1000.

    nitinsharma1983 - If your input is in bytes, then your calculations would be as follows:

    KB = bytes / 1024;
    MB = bytes / 1024 / 1024;
    GB = bytes / 1024 / 1024 / 1024;
    

    and so on.

    Then if you need to output the value of KB, MB, GB, etc. to a string and control the formatting, you can use the overload Double.ToString(String)

    This should get you started. I also hesitate to give you a full solution since this does sound like a homework question! Let us know if you need any further assistance.

    Friday, January 13, 2012 3:39 PM