• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: beginner question, NSNumber, NSDecimalAsNumber
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: beginner question, NSNumber, NSDecimalAsNumber


  • Subject: Re: beginner question, NSNumber, NSDecimalAsNumber
  • From: Ron Fleckner <email@hidden>
  • Date: Mon, 9 Nov 2009 21:01:19 +1100


On 09/11/2009, at 3:53 AM, Jay Swartzfeger wrote:

Hi all, I'm an absolute beginner to Objective-C (and programming in
general). I have lots of books on order, but I've been messing with
Xcode/Cocoa/iPhone SDK with online resources while I wait.

My question -- for my next project, I want to do a simple number <->
binary converter. Is this handled via the superclass NSNumber, or the
more specific NSDecimalAsNumber? I've been looking for examples online
and have come up empty handed. Thanks!

Jay

Hi Jay,

here is the code for a routine to go from decimal to binary representation from Stephen Prata, _C Primer Plus_ third edition:

/*
binary.c -- prints integer in binary form
From Steven Prata, "C Primer Plus", page 306-307
Prata says there, "the expression '0' + r evaluates to the character '0' if r is 0 and to the character '1' if r is 1."
*/
#include <stdio.h>
#include <stdlib.h>


void to_binary(int n);

int main(void)
{

        int num;
        printf("Enter an integer (q to quit): \n");
        while (scanf("%d", &num) == 1)
        {
                printf("Binary equivalent: ");
                to_binary(num);
                putchar('\n');
                printf("Enter an integer (q to quit): \n");
         }

         return 0;
}

void to_binary(int n)
{

        int r;
        r = n % 2;
        if (n >= 2)
            to_binary(n / 2);
        putchar('0' + r);
        return;

}

Hope that helps,

Ron

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: beginner question, NSNumber, NSDecimalAsNumber
      • From: Graham Cox <email@hidden>
References: 
 >beginner question, NSNumber, NSDecimalAsNumber (From: Jay Swartzfeger <email@hidden>)

  • Prev by Date: NSBitmapImageRepresentation image generation and saving resulting in image with much noise
  • Next by Date: Re: beginner question, NSNumber, NSDecimalAsNumber
  • Previous by thread: Re: beginner question, NSNumber, NSDecimalAsNumber
  • Next by thread: Re: beginner question, NSNumber, NSDecimalAsNumber
  • Index(es):
    • Date
    • Thread