Re: Converting Int Binary to Char String
Re: Converting Int Binary to Char String
- Subject: Re: Converting Int Binary to Char String
- From: Camillo Lugaresi <email@hidden>
- Date: Fri, 6 Jan 2006 11:50:50 +0100
On 06/gen/06, at 10:16, Jordan Evans wrote:
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.
It sounds like what you want is a C algorithm to print an integer to
a string in an arbitrary radix. You can use something like this:
#define BUF_SIZE 100
char digits[] = "0123456789ABCDEF"; /* add more digits if you need a
radix > 16 */
char buf[BUF_SIZE];
char *p = buf + BUF_SIZE;
*--p = 0;
if (n == 0) *--p = digits[0];
else while (n) {
*--p = digits[n % radix];
n /= radix;
}
At the end, p points to a C string containing the representation of n
in base radix. Code typed in Mail, no warranties, add bounds
checking. :-)
I hope I understood your intent correctly.
Camillo
_______________________________________________
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