Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

abcdefg12345

macrumors 6502
Original poster
Jul 10, 2013
281
86
i have 3 fields decimal, hexadecimal and octal and i want to convert the number typed in one field into the other 2 values but i can't tell the computer that the string I'm entering into the octal field is octal, with the hexadecimal i can use
Code:
scanHexInt
but there is no
Code:
scanOctInt
so i get wrong values when converting octal to decimal and hex
thats mt code

PHP:
#import "AppDelegate.h"

@implementation AppDelegate

@synthesize decField = _decField;
@synthesize hexField = _hexField;
@synthesize octField = _octField;

@synthesize window = _window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    
    self.decField.delegate = self;
    self.hexField.delegate = self;
    self.octField.delegate = self;
    _changing = NO;
}


- (void)controlTextDidChange:(NSNotification *)notification
{
    if ( _changing ) {
        return;
    }
    _changing = YES;

    // decimal to octal and hex

    if([notification object] == self.decField) {
        
        unsigned int n = [self.decField intValue];
        
        NSString *h = [NSString stringWithFormat:@"%x", n];
        NSString *o = [NSString stringWithFormat:@"%o", n];
        [self.hexField setStringValue: h];
        [self.octField setStringValue: o];
    }

    // octal to decimal and hex

    if([notification object] == self.octField) {
        
         unsigned int n = [self.octField intValue];
        
        NSString *h = [NSString stringWithFormat:@"%x", n];
        NSString *d = [NSString stringWithFormat:@"%d", n];
        [self.hexField setStringValue: h];
        [self.decField setStringValue: d];
    }

    // hex to octal and decimal

    if([notification object] == self.hexField) {

        NSScanner * scanner = [[NSScanner alloc] initWithString:[self.hexField stringValue]];
        
        unsigned int n = 0;
        
        [scanner scanHexInt: &n];
        
        NSString *d = [NSString stringWithFormat:@"%d", n];
        NSString *o = [NSString stringWithFormat:@"%o", n];
        [self.decField setStringValue: d];
        [self.octField setStringValue: o];
    }
    
    _changing = NO;
}

@end
 
I suggest writing the conversion code yourself. You won't always have such things pre-made and available for your use, so you should know how to make them.

If you've never written a numeric convertor before, this is a good one to start with. Octal is one of the simpler conversions, because you only need to convert digits 0-7; anything else is invalid. The conversion method can take an NSString as input and return an integer.

A more advanced version could be a category on NSScanner, but that's overly complex for this simple use, and doesn't add anything useful in this context.
 
It's generally a good exercise to write this yourself, first for binary, octal, hex, and decimal, then for arbitrary bases. Going between binary, octal, and hex is fairly straightforward because of easy grouping (3 bits per octet, 4 bits per hexit, 4 octets to 3 hexits).

You'll need a little looping, some character to numeric conversions, and some error checking. Probably a few dozen lines of code. Post your progress and we can help if things get hairy.

-Lee
 
It's generally a good exercise to write this yourself, first for binary, octal, hex, and decimal, then for arbitrary bases. Going between binary, octal, and hex is fairly straightforward because of easy grouping (3 bits per octet, 4 bits per hexit, 4 octets to 3 hexits).

You'll need a little looping, some character to numeric conversions, and some error checking. Probably a few dozen lines of code. Post your progress and we can help if things get hairy.

-Lee

been a bit busy doing something else for the past few weeks and didn't bother with this but i found out how to do it

is the following right

Code:
@synthesize decField = _decField;
@synthesize hexField = _hexField;
@synthesize octField = _octField;
@synthesize window = _window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    
    self.decField.delegate = self;
    self.hexField.delegate = self;
    self.octField.delegate = self;
    _changing = NO;
}


- (void)controlTextDidChange:(NSNotification *)notification
{
    if ( _changing ) {
        return;
    }
    _changing = YES;
    // decimal to octal and hex
    if([notification object] == self.decField) {
        
        unsigned int n = [self.decField intValue];
        
        NSString *h = [NSString stringWithFormat:@"%X", n];
        NSString *o = [NSString stringWithFormat:@"%O", n];
        [self.hexField setStringValue: h];
        [self.octField setStringValue: o];
    }
    // octal to decimal and hex
    
    if([notification object] == self.octField) {
        
//this converts the octal to decimal
        long long d = strtoll([_octField stringValue].UTF8String, NULL, 8);
        [self.decField setFloatValue:d];
        
//and this gets the new decimal value and converts it to hexadecimal
        unsigned int n = [self.decField intValue];
        NSString *h = [NSString stringWithFormat:@"%X", n];
        [self.hexField setStringValue: h];
    }
    // hex to octal and decimal
    if([notification object] == self.hexField) {

        NSScanner * scanner = [[NSScanner alloc] initWithString:[self.hexField stringValue]];
        
        unsigned int n = 0;
        
        [scanner scanHexInt: &n];
        
        NSString *d = [NSString stringWithFormat:@"%D", n];
        NSString *o = [NSString stringWithFormat:@"%O", n];
        [self.decField setStringValue: d];
        [self.octField setStringValue: o];
    }
    
    _changing = NO;
}
 
is the following right

Code:
        NSString *h = [NSString stringWithFormat:@"%X", n];
        NSString *o = [NSString stringWithFormat:@"%O", n];
}

Using the format specifiers with stringWithFormat: should work for the hex and octal conversions. It's difficult to assess the other code since declarations from the @interface are missing. However, the code in applicationDidFinishLaunching: looks potentially suspect depending on other declarations.
 
I would actually insist that you write routines to do this character by character. I would take a long and accumulate the decimal value represented by the given value, then be able to generate a string in the other bases for a binary value. IMO this will help you better understand the process. Depending on how you do it you can learn a lot about but shifting and masking, too. I.e. if you start with the decimal value in a long you can mask and shift the 3 bits at a time (either direction) for octal, and 4 bits at a time for hex. From there it's a pretty easy array lookup to get the character for that value, and append or prepend to your string.

The methods I would imagine:
long longFromOctalString(NSString *octal);
long longFromHexString(NSString *hex);
NSString *octalStringFromLong(long value);
NSString *hexStringFromLong(long value);

Then you can combine these to handle any of the three cases.

Also, you'd need to make the call on negatives or assert that only positives are allowed.

-Lee
 
I know NSScanner can convert from hexadecimal, and I think there's a printf-like specifier to convert integer values to hexadecimal, so you could NSString stringWithFormat: to make a hex number.

For octal numbers, you may need to write your own. An NSFormatter subclass can be used to automatically translate values in a UI, but is not a requirement.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.