Link to home
Start Free TrialLog in
Avatar of anthonytr
anthonytrFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Convert Byte to MB or GB


I have an application which outputs file data into a database as each file is created.  The data which is added to the database is the file size in (Bytes).  I run regular queries and report which add these fields up and give me a total size of all the documents which have been created.  Obviously, it just adds up the BYTE field so that I end up with a huge figure.  How can I then convert this figure into either MB or GB.  I understand that:

for MB it is bytes/(1024*1024)
for GB is it bytes/(1024*1024*1024)

I'm getting nowhere with this.  

Avatar of Stuart Dryden
Stuart Dryden
Flag of United Kingdom of Great Britain and Northern Ireland image

divide by 1024 for each step.

be careful about 'bytes on disk' compared to file size (in the properties of the file)
Avatar of Vitor Montalvão
Correction (divide and not multiply):
  • for MB it is bytes/1024/1024
  • for GB is it bytes/1024/1024/1024
I am thinking that divide by 1024 might be too obvious and so am wondering where other confusion might lay.

Are you taking the file size of the database container file and wondering why this does not match up to the size of the contents?

are you using some other software that could be possibly be counting up the space in used allocation units?

Where exactly are you reading the size values from and how are they described ?
- 1 Bit = Binary Digit;

- 8 Bits = 1 Byte;

- 1000 Bytes = 1 Kilobyte;

- 1000 Kilobytes = 1 Megabyte;

- 1000 Megabytes = 1 Gigabyte;

- 1000 Gigabytes = 1 Terabyte;

- 1000 Terabytes = 1 Petabyte;

- 1000 Petabytes = 1 Exabyte;

- 1000 Exabytes = 1 Zettabyte;

- 1000 Zettabyte = 1 Yottabyte;

- 1000 Yottabyte = 1 Brontobyte.

Open in new window

Avatar of anthonytr



Our Kodak software outputs a batch file with file info on.  One of the fields is document size (in Bytes).
You will have to run a few different tests, when you get the matching answer you will know you are taking the right values.

Sometimes the software that reports KB, MB and GB can play some nasty tricks on you.  as an example think of decimal stones and pounds.  What is the difference between 10 Stone 5 pounds and (on a display might look like 10, 5) and 10.5 stone.  A stone has 14 pounds and so 10.5 decimal stones is actually 10 stone 7 pounds.   If software reports 10.5 K this might or might not be (10 x 1024) + (0.5 x 1024)  you never know how it is reporting the numbers and you should ask yourself if you trust it.

Run calculations in different ways until you figure out how it is working.

At its most simple i would imagine the following.

Kodak picture is 123,456 bytes
when reported in Kb  this 'could be' 123000 divided by 1024 which gives you the whole KiloBytes plus 456 bytes extra. or it could be the accurate 123,456 divided by 1024.  test which is the correct answer and try to come up with some of your own attempts
divide by another 1024 gives you the MegaBytes
Divide by another 1024 gives you the GigaBytes

Software always has its own unique perspective on reporting bytes - the trick is to quantify what it is doing by testing it mathematically.
Avatar of aikimark
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Depending if you count via 1000x1000 or 1024x1024 look here for definitions
Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The Log() function makes the code much simpler.  I used 1000 for illustration purposes.  You can use 1024.  It wasn't clear from the comments whether the OP needed the output in Kilo or Kibi units.
Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Who is the clown who says kilobytes are always 1024?

That's why we now have an official definition,
Endorsed as IEC/SI units
1000 = Kilobytes, 1024 = Kibi
Mega & Mebi
Etc etc
anthonytr, a feedback will be appreciated.
I do not agree with the points split suggestion. IMHO isn't fair to have one Expert getting half of the points  and 6 others splitting between them the other half of points.
I strongly disagree too with the point allocation.  Firstly there were only a couple of experts that the Antony (the user who asked the question) interacted with.  Gerald not being one of them. Secondly Gerald did not interact with the originator, his first comment was a repeat and his second was calling another expert a clown and being pedantic about SI unit expression.  IMHO Gerard should be awarded ZERO.

My comments were helpful, were the first to reply, interacted with the user, gave the user understanding of the problem, had foresight into what the users actual problem was, gave a methodology the user to investigate further, my comments were referenced by other experts as good information.

I would suggest 250 / 250 points given to both myself and Vitor.  We both were gave the only answers that interacted with the user while they were still involved in the question.  All other answers were either a repeat of what we said or advanced information far beyond the users capability of understanding.

My specific objection as to the allocation reasoning given by Gerald is that the user was not helped by knowing that KiloBytes were KiBi - they did not need to know that, the user wanted help on the information they had provided.
I would recommend that the points be split between aikimark and crystal, since we their comments were the only two with solution code to the problem.
Code?  the user was not helped by code nor would they have been able to use it to understand their problem.

The main problem this website experiences is experts trying to outbid each other with more and more harder complex technical information, each more complex than the next.  This does not help a person wanting assistance to solve their problem.

I am a technical support engineer, despite being capable of giving far more complex information on a subject I pitch the level of my response to assist a person in reaching their technical goals.

This website is called Experts Exchange, i believe because it is information exchanged between experts and those requiring help.  The website is not called "Battle of the Experts", where he who can give more complex information wins.

Please in future pitch your answer at the level of the person asking the question - this website is not 'just' a knowledge base - it should be aimed at helping people.  If you want to add more high level information on a subject than is required can i suggest that you make an edit to a Wiki ?
Avatar of crystal (strive4peace) - Microsoft MVP, Access
crystal (strive4peace) - Microsoft MVP, Access

re: Kilobyte vs Kibi.
Kibi is a fairly new term.  In the beginning of PCs, more than 30 years ago when I began using them, a kilobyte was 1024 bytes, in relationship to computers. Once you think you know a term, one does not tend to look it up again, so I learned something too.  The original post, however, specified 1024.

I agree that we are here to help each other.  Everyone should be spoken to cordially. Mud-slinging doesn't help anyone.
Agreed in regards to the KB / Kibi issue.  It was one of the more ridiculous standard ratifications i did not agree with when it came out - but who are we to disagree with an expert panel.

They ignored the fact that we represented bits with a lowercase b, bytes with an uppercase B.

it was easy to understand bps and Bps, Kbps and KBps.  the wider IT community evolved to this understanding naturally.  Computer techies understood this meant powers of 1024.  I guess the expert panel just wanted to guard the true meaning of Kilo / Mega etc etc.

I understand the reasons behind it even though it was unwelcome.  I guess they must be quite upset that i have only ever seen a couple of examples EVER of people using the correct notation for the units.
Rob, I still think that Stuart and I fixed the author's formula in our first comments. There's no doubts that next comments from other Experts added some value but the exact issue is that the author had the wrong formula.