convert octal to decimal and hexadecimal

Discussion in 'Mac Programming' started by abcdefg12345, Sep 17, 2014.

  1. abcdefg12345 macrumors regular

    abcdefg12345

    Joined:
    Jul 10, 2013
    #1
    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 *= [NSString stringWithFormat:@"%x"n];
            
    NSString *= [NSString stringWithFormat:@"%o"n];
            [
    self.hexField setStringValueh];
            [
    self.octField setStringValueo];
        }

        
    // octal to decimal and hex

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

        
    // hex to octal and decimal

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

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

    @
    end
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    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.
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    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
     
  4. abcdefg12345 thread starter macrumors regular

    abcdefg12345

    Joined:
    Jul 10, 2013
    #4
    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;
    }
     
  5. briloronmacrumo macrumors 6502

    briloronmacrumo

    Joined:
    Jan 25, 2008
    Location:
    USA
    #5
    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.
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    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
     
  7. Madd the Sane macrumors 6502

    Madd the Sane

    Joined:
    Nov 8, 2010
    Location:
    Utah
    #7
    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.
     

Share This Page