Re: beginner question, NSNumber, NSDecimalAsNumber
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