Re: Base Conversion?
Re: Base Conversion?
- Subject: Re: Base Conversion?
- From: Steven Spencer <email@hidden>
- Date: Mon, 18 Jul 2005 13:31:29 +0100
Hi,
I've converted the following from some old Visual Basic code I wrote to do base conversion.
Typed outside of XCode - Not checked if it compiles.
+ (NSString *)stringWithNumber:(unsigned int)number forBase:(unsigned int)base
{
char *strNum = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
NSMutableArray *stack = [NSMutableArray array];
NSMutableString *str = [NSMutableString string];
NSEnumerator *enm;
NSNumber *n;
if (base < 2 || base > 36) {
return [NSString stringWithFormat:@"Base %u unsupported", base];
} else {
if (0 == number) return @"0";
while (number != 0) {
[stack addObject:[NSNumber numberWithUnsignedInt:numberšse]];
number = number / base;
}
enm = [stack reverseObjectEnumerator];
while (n = [enm nextObject]) {
[str appendFormat:@"%c", strNum[[n unsignedIntValue]]];
}
return [[str copy] autorelease];
}
}
- Steve Spencer
-------------------------------------
So I need to write my own.
Thanks, that's what I needed to know. =)
Dave
On Jul 16, 2005, at 10:36 PM, Scott Ribe wrote:
> Assuming you don't mind converting to & from plain C strings, for
> parsing,
> you can use strtol. For display I don't know of a built-in
> function. The
> usual approach is have a static array of 23 characters, loop
> building the
> string up by num# and setting num=num\23 each time through...
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden