[ create a new paste ] login | about

Link: http://codepad.org/ZlVskhoY    [ raw code | output | fork ]

slevy1ster - C, pasted on Sep 3:
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
int main()
{
 //  PHPAPI int _php_math_basetozval(zval *arg, int base, zval *ret)

    long num = 0;
    double fnum = 0;
    int i;
    int base = 16;
    int mode = 0;
    char c, *s;
    long cutoff;
    int cutlim;


    s = "0.5";

    cutoff = LONG_MAX / base;
    cutlim = LONG_MAX % base;

    for (i = 3; i > 0; i--) {
        c = *s++;

        /* might not work for EBCDIC */
        if (c >= '0' && c <= '9')
            c -= '0';
        else if (c >= 'A' && c <= 'Z')
            c -= 'A' - 10;
        else if (c >= 'a' && c <= 'z')
            c -= 'a' - 10;
        else
            continue;  // will skip '0' and later '.'

        if (c >= base)
            continue;

        switch (mode) {
        case 0: /* Integer */
            if (num < cutoff || (num == cutoff && c <= cutlim)) {
                num = num * base + c;
                break;
            } else {
                fnum = num;
                mode = 1;
            }
            /* fall-through */
        case 1: /* Float */
            fnum = fnum * base + c;
        }
    }

    if (mode == 1) {
        printf("%f",fnum);
    } else {
        printf("%d", num);
    }
    return 0;
}


Output:
1
5


Create a new paste based on this one


Comments: