Re: Outputting an unsigned long long
Re: Outputting an unsigned long long
- Subject: Re: Outputting an unsigned long long
- From: Sherm Pendley <email@hidden>
- Date: Thu, 20 Jun 2002 22:57:28 -0400
On Thursday, June 20, 2002, at 10:07 PM, Clark Mueller wrote:
I am having a problem outputting an unsigned long long, and I am at a
loss as to how to remedy the problem. I create an NSString using the
ANSI formatters:
[NSString stringWithFormat:@"%.1f GB (%d bytes)",
(float)totalPhysical/1024/1024/1024, (int)totalLogical]
I see two issues here:
First, you're using the format string %d, which is used for signed ints.
The format string for unsigned ints is %u. My C99 reference mentions
%lld and %llu for signed and unsigned long longs, as well - but I don't
see any reference to that in the printf man page, so it may not yet be
supported in the compiler currently shipping with OS X.
Second, you're typecasting totalLogical, which I assume is declared as
an unsigned long long, to (int), causing it to be treated as a signed
long int. A typecast doesn't actually convert the variable, it just
tells the compiler to treat it as something other than what it's been
declared as. In this case, it tells it to use the high order bit to
indicate positive/negative values. So, once the number is large enough
that the high order bit is set, the compiler sees it as a negative value.
You can see the same thing in reverse, if you assign -1 to a signed int,
and then typecast it as an unsigned int - the result will be a very
large number.
In short, if totalLogical is declared as "unsigned long long," lose the
typecast and try using %llu in your format string. If %llu doesn't work,
try using just %u. If that doesn't work, you may want to try the new
GCC3 compiler in the beta developers tools - I've heard that C99 support
has been much improved.
Hope this helps!
sherm--
Never put off until tomorrow what you can do today. There might be a law
against it by that time.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.