Converting Int Binary to Char String
Converting Int Binary to Char String
- Subject: Converting Int Binary to Char String
- From: Jordan Evans <email@hidden>
- Date: Fri, 6 Jan 2006 01:16:18 -0800 (PST)
Do anyone here know of the fastest algorithm in
Objective-C code for changing a 32 bit integer into a
character string?
I'd prefer to see one that only used the very basic
operators, such as: shifts, bitwise operators,
assignments, and is equal or is not equal (>>, <<, &,
|, ^, =, and !=).
vs.
these types:
<=, >=, mod, multiplication, division, addition, etc.
I do have to add this type of algorithm to a Cocoa
class I am creating, because messaging for NSNumber
would be slow, because of the implementation demands I
have.
Here's my simple incrementer. It's fine if I want to
go through an entire possible number of combinations
of a multibase string value, but it's not good at
finding one sequence, because it has to travel through
every possibility to get there, which is what I'm
trying to avoid.
//
// Radix.h
// RadixProject
//
// Created by Jordan Evans on 12/1/05.
// Copyright 2005 __MyCompanyName__. All rights
reserved.
//
#import <Cocoa/Cocoa.h>
@interface Radix : NSObject
{
id arraySource;
int omegaElement;
int sequence;
int possibilities;
char *sequenceString;
struct radixSet
{
int element;
int alphaElement;
int omegaElement;
BOOL setIsString;
id setPtr;
};
struct radixSet *sets;
int v;
}
-initWithSets:(NSArray *)objects;
-(id)sequence: (int)requestedSequence;
@end
#import "Radix.h"
@implementation Radix
-initWithSets:(NSArray *)objects
{
self = [super init];
if (self != nil)
{
int i, count;
possibilities = 1;
arraySource = objects;
count = [objects count];
sets = (struct radixSet *)malloc (sizeof(struct
radixSet)*count);
id lastObject;
for(i=0;i<count;i++)
{
sets->setPtr = [objects objectAtIndex:i];
lastObject = [sets->setPtr lastObject];
if([sets->setPtr isKindOfClass:[NSString class]])
sets->omegaElement = [sets->setPtr count]-1;
else
sets->omegaElement = [sets->setPtr count]-1;
possibilities *= sets->omegaElement;
sets++;
}
sets -= count;
}
return self;
}
- (void)dealloc;
{
free(sets);
[super dealloc];
}
-(id)sequence: (int)requestedSequence
{
int i, count;
requestedSequence -= 1;
if(requestedSequence)
{
do
{
do
{
if( sets[v].element == sets[v].omegaElement )
{
sets[v].element = 0;
v++;
}
}while( sets[v].element == sets[v].omegaElement );
sets[v].element += 1;
v = 0;
sequence++;
if(sequence == requestedSequence)
break;
}
while( sequence != requestedSequence );
}
count = [arraySource count];
NSMutableArray *sequenceArray = [[NSMutableArray
alloc] init];
for( i=0; i<count; i++ )
[sequenceArray addObject: [sets[i].setPtr
objectAtIndex:sets[i].element] ];
return sequenceArray;
}
@end
#import <Foundation/Foundation.h>
#import "Radix.h"
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]
init];
NSString *num1 = [NSString stringWithString:@"0"];
NSString *num2 = [NSString stringWithString:@"1"];
NSString *num3 = [NSString stringWithString:@"2"];
NSString *num10 = [NSString stringWithString:@"3"];
NSArray *set1 = [NSArray arrayWithObjects: num1,
num2, num3, num10, nil];
NSString *num4 = [NSString stringWithString:@"0"];
NSString *num5 = [NSString stringWithString:@"1"];
NSArray *set2 = [NSArray arrayWithObjects: num4,
num5, nil];
NSString *num7 = [NSString stringWithString:@"0"];
NSString *num8 = [NSString stringWithString:@"1"];
NSString *num9 = [NSString stringWithString:@"2"];
NSArray *set3 = [NSArray arrayWithObjects: num7,
num8, num9, nil];
int i, j;
id recSeqArray;
Radix *myRadix = [[Radix alloc] initWithSets:[NSArray
arrayWithObjects: set1, set2, set3, nil]];
int count;
for( i=10; i<=10; i++ )
{
recSeqArray = [myRadix sequence:i];
NSString *s = [NSString
stringWithString:[recSeqArray className]];
NSString *test = [recSeqArray objectAtIndex:0];
printf("sequence %i\n", i );
count = [recSeqArray count] - 1;
for( j=count; j>=0; j-- )
printf("%s", [[recSeqArray objectAtIndex:j]
cString]);
printf("\n");
}
[recSeqArray release];
[myRadix release];
[pool release];
return 0;
}
__________________________________________
Yahoo! DSL Something to write home about.
Just $16.99/mo. or less.
dsl.yahoo.com
_______________________________________________
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