Re: Outputting an unsigned long long
Re: Outputting an unsigned long long
- Subject: Re: Outputting an unsigned long long
- From: Clark Mueller <email@hidden>
- Date: Thu, 20 Jun 2002 21:54:23 -0600
As a matter of fact, all of the above apply. I've used %lld, llu, u, with or without the cast, and none of them seem to work. I am using the beta tools as well. The number that I know causes this problem is "8575757575". Something else that isn't clear from my original question is exactly what "totalPhysical" is. It is also an unsigned long long, and it is of approximately the same value (basically it's the kind of difference you'd see between the logical and physical sizes of a folder - not anything particularly significant, most of the time). The only difference is that it is casted to a float and output as x.x GB, not going past one decimal. The same can be done with the totalLogical value, and it comes out as 7.6GB, which is the correct logical size of the folder in question.
Thanks,
Clark
On Thursday, June 20, 2002, at 08:57 PM, Sherm Pendley wrote:
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.
Clark Mueller
email@hidden
---
http://www.finikin.com/
http://www.finikin.com/clark/
---
Computer programmers don't byte, they nibble a bit.
_______________________________________________
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.