convert NSData Length from bytes to megs
Asked Answered
C

3

41

I am trying to NSLog the number of megs my NSData object is however currently all I can get is bytes by using

NSLog(@"%u", myData.length);

So how would I change this NSLog statement so I can see something like

2.00 megs

any help would be appreciated.

Crinum answered 10/12, 2012 at 22:15 Comment(1)
How would I format it so it appears with two decimal places after it.Crinum
S
128

There are 1000 bytes in a kilobyte and 1000 kilobytes in a megabyte, so...

NSLog(@"File size is : %.2f MB",(float)myData.length/1000.0f/1000.0f);

Mind you, this is a simplistic approach that doesn’t handle particularly large or small numbers. A more generalized solution is to use Apple’s NSByteCountFormatter:

Or for OS X 10.8+ and iOS 6+

NSLog(@"%@", [[NSByteCountFormatter new] stringFromByteCount:data.length]);

In Swift:

print(ByteCountFormatter().string(fromByteCount: Int64(data.count)))
Sanitize answered 10/12, 2012 at 22:21 Comment(3)
Cool, thanks a bunch that worked.. I was getting confused thinking I have to have brackets etc but this worked perfectly! :)Crinum
If you need to show this to the user then do not use a string format. Instead, use NSNumberFormatter. This will format the for the user's locale.Disrespect
quick and hand. it was helpfulTurbinate
M
18

For Swift 3, in Mb:

let countBytes = ByteCountFormatter()
countBytes.allowedUnits = [.useMB]
countBytes.countStyle = .file
let fileSize = countBytes.string(fromByteCount: Int64(dataToMeasure!.count))

print("File size: \(fileSize)")
Medullary answered 25/4, 2017 at 20:17 Comment(0)
M
7

With Swift 5.1 and iOS 13, you can use one of the 5 following ways to solve your problem.


#1. Using ByteCountFormatter's string(fromByteCount:countStyle:) class method

The following sample code shows how to implement string(fromByteCount:countStyle:) in order to print a file size by automatically converting bytes to a more appropriate storage unit (e.g. megabytes):

import Foundation

let url = Bundle.main.url(forResource: "image", withExtension: "png")!
let data = try! Data(contentsOf: url)

let byteCount = data.count
print(byteCount) // prints: 2636725

let displaySize = ByteCountFormatter.string(fromByteCount: Int64(byteCount), countStyle: .file)
print(displaySize) // prints: 2.6 MB

#2. Using ByteCountFormatter's string(fromByteCount:) method

The following sample code shows how to implement ByteCountFormatter's string(fromByteCount:) in order to print a file size by manually converting bytes to megabytes:

import Foundation

let url = Bundle.main.url(forResource: "image", withExtension: "png")!
let data = try! Data(contentsOf: url)

let byteCount = data.count
print(byteCount) // prints: 2636725

let formatter = ByteCountFormatter()
formatter.allowedUnits = [.useMB]
formatter.countStyle = .file
let displaySize = formatter.string(fromByteCount: Int64(byteCount))
print(displaySize) // prints: 2.6 MB

#3. Using ByteCountFormatter's string(from:countStyle:) class method and Measurement

The following sample code shows how to implement string(from:countStyle:) in order to print a file size by automatically converting bytes to a more appropriate storage unit (e.g. megabytes):

import Foundation

let url = Bundle.main.url(forResource: "image", withExtension: "png")!
let data = try! Data(contentsOf: url)

let byteCount = data.count
print(byteCount) // prints: 2636725

let byteSize = Measurement(value: Double(byteCount), unit: UnitInformationStorage.bytes)
let displaySize = ByteCountFormatter.string(from: byteSize, countStyle: .file)
print(displaySize) // prints: 2.6 MB

#4. Using ByteCountFormatter's string(from:) method and Measurement

The following sample code shows how to implement ByteCountFormatter's string(from:) and Measurement in order to print a file size by manually converting bytes to megabytes:

import Foundation

let url = Bundle.main.url(forResource: "image", withExtension: "png")!
let data = try! Data(contentsOf: url)

let byteCount = data.count
print(byteCount) // prints: 2636725

let byteSize = Measurement(value: Double(byteCount), unit: UnitInformationStorage.bytes)
let formatter = ByteCountFormatter()
formatter.allowedUnits = [.useMB]
formatter.countStyle = .file
let displaySize = formatter.string(from: byteSize)
print(displaySize) // prints: 2.6 MB

#5. Using MeasurementFormatter's string(from:) method and Measurement

The following sample code shows how to implement Measurement and MeasurementFormatter's string(from:) in order to print a file size by manually converting bytes to megabytes:

import Foundation

let url = Bundle.main.url(forResource: "image", withExtension: "png")!
let data = try! Data(contentsOf: url)

let byteCount = data.count
print(byteCount) // prints: 2636725

let byteSize = Measurement(value: Double(byteCount), unit: UnitInformationStorage.bytes)
let convertedSize = byteSize.converted(to: .megabytes)
let formatter = MeasurementFormatter()
let displaySize = formatter.string(from: convertedSize)
print(displaySize) // prints: 2.637 MB
Microdot answered 24/6, 2019 at 18:19 Comment(2)
How to get the size in mega as a Double value without MB?Encroachment
@Encroachment You can set ByteCountFormatter's includesUnit property to false in order to include only the count.Microdot

© 2022 - 2024 — McMap. All rights reserved.