473,738 Members | 9,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calculating Bytes/KB/MB/Gb...

Hi
I have long variable containing number of stored bytes.
I want to display Bytes , KB, MB, GB depending of the size of this variable
as a string.
Does framework provide any class for such a fast calculation or i have to
divide by 1024, check if result is zero, if not divide again and so on...?
Thanks
PK
Oct 4 '06 #1
12 17748
PiotrKolodziej <pi************ *@gmail.comwrot e:
Hi
I have long variable containing number of stored bytes.
I want to display Bytes , KB, MB, GB depending of the size of this variable
as a string.
Does framework provide any class for such a fast calculation or i have to
divide by 1024, check if result is zero, if not divide again and so on...?
bytes / 1024 == KB
bytes / 1024^2 == MB
bytes / 1024^3 == GB
bytes / 1024^4 == TB

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Oct 4 '06 #2
bytes / 1024 == KB
bytes / 1024^2 == MB
bytes / 1024^3 == GB
bytes / 1024^4 == TB
It's not the answer for my question.
Oct 4 '06 #3
PiotrKolodziej <pi************ *@gmail.comwrot e:
>
>bytes / 1024 == KB
bytes / 1024^2 == MB
bytes / 1024^3 == GB
bytes / 1024^4 == TB

It's not the answer for my question.
I think my answer was "No".

You can write your own FormatProvider if you wish.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Oct 4 '06 #4
On 04/10/2006 in message <7e************ *************** @news.chello.pl >
PiotrKolodziej wrote:
>Hi
I have long variable containing number of stored bytes.
I want to display Bytes , KB, MB, GB depending of the size of this
variable as a string.
Does framework provide any class for such a fast calculation or i have to
divide by 1024, check if result is zero, if not divide again and so on...?
Thanks
PK
You have to do it yourself Piotr :-(

Do it once and keep it somewhere safe, it's a useful function to have!

--
Jeff Gaines
Oct 4 '06 #5
There is nothing in the framework for this (that I know of), mainly
because:
1) It have a very specific use.
2) the calculation itself is trivial
3) it's troubled by a number of formatting issues (commas or spaces?
KB or Ko?)

PiotrKolodziej wrote:
Does framework provide any class for such a fast calculation or i have to
divide by 1024, check if result is zero, if not divide again and so on...?
Oct 4 '06 #6
I think my answer was "No".

Your answer was:
bytes / 1024 == KB
bytes / 1024^2 == MB
bytes / 1024^3 == GB
bytes / 1024^4 == TB
You can write your own FormatProvider if you wish.
I knew it and the question was due to something else.
Oct 4 '06 #7
Do it once and keep it somewhere safe, it's a useful function to have!

Indeed.
Anyway thanks you for your posts.
Oct 4 '06 #8
PiotrKolodziej <pi************ *@gmail.comwrot e:
Hi
I have long variable containing number of stored bytes.
I want to display Bytes , KB, MB, GB depending of the size of this variable
as a string.
Does framework provide any class for such a fast calculation or i have to
divide by 1024, check if result is zero, if not divide again and so on...?
switch((int)(Sy stem.Math.Log(b ytecount) / System.Math.Log (1024)))
{
case 0:
return @"bytes";
case 1:
return @"KB";
case 2:
return @"MB";
case 3:
return @"GB";
case 4:
return @"TB";
default:
reutrn @"REALLY HUGE!";
}
--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Oct 4 '06 #9
(int)(System.Ma th.Log(bytecoun t) / System.Math.Log (1024))

Although log will work (rounding permitting), it seems overkill here...
personally I'd just keep ">10"-ing until I get a zero, and use the
previous value for the number, keeping track of the number of shifts
for the kb, Mb, Gb, Tb etc... It should be a lot less FLOPs... (i.e.
none)

Marc

Oct 5 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1688
by: Amit | last post by:
Hi: I'm trying to manually compute the ISIZE in REORGCHK for indexes in DB2 v8 (fp 4). Based on the documentation in Administrative Guide for v8, for non lob fields ISIZE should be the aggregate of avg datasize of the index columns + 2 bytes (1 byte for v7) overhead for each varchar and vargraphic column and 1 byte overhead for each null column. I use AVGCOLLEN in syscat.columns for the column size, and add 2 bytes
2
2534
by: kuhni | last post by:
Hi everybody! After searching newsgroups for a couple of hours, I now try asking directly (running the risk of asking again the same question). My problem is to predict when the size of the database (1GB I expect) is over 1GB by calculating the maximal number of data sets (tuple) added: 1. Is the following calculation for estimating the mdb file size in Access 97 correct: 1 integer field = 4 Byte
5
11952
by: sugaray | last post by:
Hi, my problem with calculating the size of an array is when I pass an array as a parameter to a function which perform the calculation, the result never comes right, like below: int SizeOfArray(int a) { return (sizeof(a)/sizeof(a)); } main() { int a={1,2,3}; printf("%d\n",SizeOfArray(a));
5
3484
by: Lars Netzel | last post by:
Hello! I have a number of files to copy. I have to total Amount of files and the Total FileSize of this operation (for example 3453 files, 4065.4 Mb)... How can I calculate how much time this will take so the user have an Idea of how much time it will take to copy? This will also be something to base a progressbar on. regards /Lars
13
14270
by: Martin Herbert Dietze | last post by:
Hi, I need to calculate the physical length of text in a text input. The term "physical" means in this context, that I consider 7bit-Ascii as one-byte-per character. Other characters may be longer, e.g. cyrillic would be 2 bytes per character. Is there a safe and easy way to notice non-7bit-Ascii input? Cheers,
8
1975
by: LEM | last post by:
Hi all, Perhaps an easy question, but I haven't been able to find the solution. Let's suppose that I have this: byte a; a=0x01; a=0x6B;
18
9176
by: gregpinero | last post by:
It seems like this would be easy but I'm drawing a blank. What I want to do is be able to open any file in binary mode, and read in one byte (8 bits) at a time and then count the number of 1 bits in that byte. I got as far as this but it is giving me strings and I'm not sure how to accurately get to the byte/bit level. f1=file('somefile','rb')
0
1293
by: hiitsmedear | last post by:
Hi, I am working on memory leaks for a windows service and the way i am calculating the leaks is appears tobe somewhat weired to me, Can someone please suggest me that is this the correct way to do the calculation or some other alternate way available to do the same. I run the service for 10 hours(H). I log the Private bytes using Perfmon. than i extract the maximum(X) and minimum(N) of Private bytes from the Log.
10
5245
freddieMaize
by: freddieMaize | last post by:
Hi there, I need to hit few list of sites from my application and find the response time of those site and sort them (which ever is opening first will sit at the top). I have done the same using java (server side). Now my requirement is do in the client side (AJAX possibly). I’m having two challenges. Below is what I have done so far, <script> function call() { var d = new Date(); var xmlHttp;
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.