Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert bits per second (bps) to a human readable size format

How can i convert/calculate bits per second (bps) to a readable size format like 10 Mbps, 7 Gbps, 5 Tbps, 4 Pbps, 3 Ebps...etc in iOS.

Best

like image 853
mgyky Avatar asked Oct 28 '25 02:10

mgyky


1 Answers

Objective-C

- (NSString *)convertBitrateToHumanReadable:(long long)bytes {
    NSByteCountFormatter * formatter = [[NSByteCountFormatter alloc] init];
    return [formatter stringFromByteCount:bytes];
}

Swift 5.1

func convertBitrateToHumanReadable(bytes: Int64) -> String { ByteCountFormatter().string(fromByteCount: bytes) }

Swift 5

func convertBitrateToHumanReadable(bytes: Int64) -> String {
    let formatter = ByteCountFormatter()
    return formatter.string(fromByteCount: bytes)
}

You can append ps (per second) to result if you wish.

note that this answer is based on bytes instead of bits and each byte equals 8 bits....

like image 85
Mojtaba Hosseini Avatar answered Oct 30 '25 16:10

Mojtaba Hosseini