Re: Strange issue when converting from hex to float
Re: Strange issue when converting from hex to float
- Subject: Re: Strange issue when converting from hex to float
- From: Sherm Pendley <email@hidden>
- Date: Thu, 21 Jul 2005 16:04:23 -0400
On Jul 21, 2005, at 2:57 PM, Andy Lee wrote:
I don't know my operator precedences offhand
Assignment (=, +=, *=, etc.) is second-lowest precedence, just above
the comma.
It's actually pretty easy to verify how it *should* work:
Sherm-Pendleys-Computer:~ sherm$ cat testop.c
#include <stdio.h>
int main(int argc, char **argv) {
int a = 200;
float result = a/255.0;
printf("%f\n", result);
}
Sherm-Pendleys-Computer:~ sherm$ gcc -o testop testop.c
Sherm-Pendleys-Computer:~ sherm$ ./testop
0.784314
There's obviously more happening in Ian's case than meets the eye.
Reading the man page for atoi(), I see nothing that indicates that it
understands hex strings. Furthermore, it states there that atoi() is
deprecated in favor of strtol() - which accepts a "base" argument.
Comparing the two in action, I get:
Sherm-Pendleys-Computer:~ sherm$ cat testop.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int a = atoi("0xee");
int b = strtol("0xee", NULL, 16);
float result_a = a/255.0;
float result_b = b/255.0;
printf("A=%f B=%f\n", result_a, result_b);
}
Sherm-Pendleys-Computer:~ sherm$ gcc -o testop testop.c
Sherm-Pendleys-Computer:~ sherm$ ./testop
A=0.000000 B=0.933333
Seems rather obvious now - atoi() doesn't grok hex strings.
sherm--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
_______________________________________________
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