• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Strange issue when converting from hex to float
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Re: Strange issue when converting from hex to float (From: Ian was here <email@hidden>)
 >Re: Strange issue when converting from hex to float (From: Andy Lee <email@hidden>)

  • Prev by Date: Re: Sorting Array Problems
  • Next by Date: Bundles and installers...
  • Previous by thread: Re: Strange issue when converting from hex to float
  • Next by thread: Re: Strange issue when converting from hex to float
  • Index(es):
    • Date
    • Thread