Advertisement

02.21.2008 at 09:37PM PST, ID: 23183623 | Points: 500
[x]
Attachment Details

Convert bytes to kb/mb

Asked by TheMaximumWeasel in Cocoa Programming Language, Miscellaneous Programming, Objective-C Programming Language

I have an integer that holds an amount of bytes. How would I make a function that returns an NSString with a value of kb or mb depending on what is appropriate.

So if the value of bytes is over 1024 show in KB and if its over 1024 KB show it in MB. Also I need it to round to one decimal place.

Examples:
55803 returns @"54.5 KB"
3671316 returns @"3.5 MB"
362 returns @"362 B" <- No decimals if in bytes :p

Thanks,
MaxStart Free Trial
[+][-]02.21.2008 at 09:48PM PST, ID: 20954968

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.21.2008 at 10:00PM PST, ID: 20955008

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.22.2008 at 04:23AM PST, ID: 20956511

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.22.2008 at 08:43PM PST, ID: 20963700

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.23.2008 at 04:22AM PST, ID: 20964683

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.23.2008 at 07:18AM PST, ID: 20965209

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.23.2008 at 08:07AM PST, ID: 20965393

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.23.2008 at 09:03AM PST, ID: 20965637

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.25.2008 at 04:11PM PST, ID: 20980602

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.26.2008 at 08:30AM PST, ID: 20985835

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
02.21.2008 at 09:48PM PST, ID: 20954968
Simple, divide it by 1024 recursively, until the result is less than zero (0) then show the previous result

3671316 / 1024 = 3585,26953125 / 1024 = 3,501239776611328125

3,501239776611328125 /1024 = 0,0034191794693470001220703125

then you show 3,501239776611328125, but you can round it to decimal parts, then: 3.5

Count the number of times you divide, for the first division append the KB, for the second, append MB, for the third GB, and so...

 
02.21.2008 at 10:00PM PST, ID: 20955008
how does divinding make a number less than 0? Can you write the function? I am a beginner at this.

Max
 
02.22.2008 at 04:23AM PST, ID: 20956511
sry!
no less than zero, less than 1.

my bad.
 
02.22.2008 at 08:43PM PST, ID: 20963700
ok this instance method compiles perfectly, but it does not work and I can't figure it out.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
- (NSString *)reduceBytes:(long long)bytes {
	int type = 0;
	long long reducedBytes = bytes;
	while(reducedBytes > 1024) {
		reducedBytes = bytes / 1024;
		type++;
	}
	NSString *returnString;
	switch(type) {
		case 0:
			returnString = [ NSString stringWithFormat: @"%lld", reducedBytes ];
			break;
		case 1:
			returnString = [ NSString stringWithFormat: @"%f KB", (float)(((int)(reducedBytes * 10)) /10) ];
			break;
		case 2:
			returnString = [ NSString stringWithFormat: @"%f MB", (float)(((int)(reducedBytes * 10)) /10) ];
			break;
		case 3:
			returnString = [ NSString stringWithFormat: @"%f GB", (float)(((int)(reducedBytes * 10)) /10) ];
			break;
	}
	return returnString;
}
Open in New Window
 
02.23.2008 at 04:22AM PST, ID: 20964683
1. is not reducedbytes>1024, is reducedbytes>1

2. between line 4 and 5 add prev_reducedBytes=reducedBytes;
3. Line 5 Should be reducedBytes=reducedBytes / 1024;
4. between line 7 and 8 add type=type-1;   since the while will add an extra type cause will check in the next loop.

5. Inside the case, use pre_reducedBytes instead of reducedBytes.

Hope it works
 
02.23.2008 at 07:18AM PST, ID: 20965209
if I check to see if its greater than 1024, I don't have to do the prev_reducedBytes or the type-1 crap.

Max
 
02.23.2008 at 08:07AM PST, ID: 20965393
It is not crap, be more respectful with someone trying to help.
 
02.23.2008 at 09:03AM PST, ID: 20965637
No, I wasn't trying to insult you, I am sorry. When I mean crap I mean like extra code that is not needed.

Max

Here is what I got working:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
NSString *reduceBytes(unsigned long bytes) {
	const char *suffixTable[] = {"bytes", "KB", "MB", "GB", "TB", NULL};
	/*
	if (bytes == 1) {
		return @"1 byte";
	}
	*/
	int suffixIdx = 0;
	double reducedBytes = (double)bytes;
	while (reducedBytes >= 1000.0 && suffixTable[suffixIdx + 1] != NULL) {
		suffixIdx++;
		reducedBytes /= 1024.0;
	}
	return [ NSString stringWithFormat: @"%.1f %s", reducedBytes, suffixTable[suffixIdx] ];
}
Open in New Window
 
02.25.2008 at 04:11PM PST, ID: 20980602
np ;)

a little translation misunderstanding
 
02.26.2008 at 08:30AM PST, ID: 20985835
All Concerned (Asker and Experts),
 
New Feature Alert!
 
Please do not make any post of any kind after a "Closing Request" has been started by the asker - unless you have a specific objection.
 
Any post by anyone will stop the automated process and force the manual intervention of a Moderator/Admin.
 
If anyone has questions about the process, please post them here: http://www.experts-exchange.com/Q_23151688.html and not in this question.
 
TheMaximumWeasel - please start the process again and thank you for using it. When we all get used to the process, it will greatly improve the 'abandoned' question problem.
If you have specific responses or posts you need to make, please do so before starting the process again.
 
Vee_Mod
Experts Exchange Moderator
Link to CS-G post:
http://www.experts-exchange.com/Q_23192143.html
 
 
20080716-EE-VQP-32 / EE_QW_2_20070628