Tuz
Tuz

Reputation: 1980

From megabytes /GB to bytes in node

I see always a solution for converting bytes to MB or GB

is there a proper solution to convert MB to bytes

for now, I am using as follows:

 MB * Math.pow(1024, 2);

but if I would like to convert 10GB, then I set 10 GB = 10000 MB and the result in bytes is different

thanks

Upvotes: 2

Views: 3866

Answers (2)

Shiv Kumar
Shiv Kumar

Reputation: 71

1 KiB = 1024 Bytes

1 MeB = 1024 KiB = 10242 Bytes

1 GiB = 1024 MeB = 10242 KiB = 10243 Bytes

More precisely

1 KiB = 210 Bytes

1 MeB = 210 KiB = 220 Bytes

1 GiB = 210 MeB = 220 KiB = 230 Bytes

Hope it helps.

SizeInBytes = SizeInGiB * Math.pow(1024, 3);

SizeInBytes = SizeInMeB * Math.pow(1024, 2);

Upvotes: 6

Slawomir Dziuba
Slawomir Dziuba

Reputation: 1325

The difference is that you calculate the value in GiB and compare with GB. Unfortunately the usual abbreviation GB is often used to mean GiB. 1GiB (gibbyte) = 1,073,741,824 byte and 1GB (gigabyte) = 1,000,000,000 bytes.

Upvotes: 4

Related Questions