Example #1
0
int main()
{
    unsigned oct, dec;
    printf("Enter a decimal number:\n");
    scanf(" %u%*c", &dec);
    tooct(dec);
    tobin(dec);
    printf("Enter an octal number:\n");
    scanf(" %u%*c", &oct);
    oct2bin(oct);
    return 0;
}
ostream _FAR & ostream::operator<< (long l)
{
    char buf[MaxCharsInLong];   // result of conversion
    char *prefix = 0;           // displayed numeric prefix string
    char *p;

    // find conversion base
    int base = (flags() & ios::hex) ? 16 : ((flags() & ios::oct) ? 8 : 10);

    // do we treat this as negative?  (oct and hex are unsigned)
    int neg = base == 10  &&  l < 0;

    // value to use, exclusive of sign
    unsigned long ul = neg ? -l : l;

    if( base == 10 )
        {
        p = todec(buf + MaxCharsInLong - 1, ul);

        // compute any sign prefix
        if( ul )
            if( neg )
                prefix = "-";
            else if( flags() & ios::showpos )
                prefix = "+";
        }
    else if( base == 16 )
        {
        int upper = (flags() & ios::uppercase) != 0;
        p = tohex(buf + MaxCharsInLong - 1, ul, upper);

        // compute any base prefix
        if( flags() & ios::showbase )
            prefix = upper ? "0X" : "0x";
        }
    else /* base == 8 */
        {
        p = tooct(buf + MaxCharsInLong - 1, ul);

        // compute any base prefix
        if( flags() & ios::showbase )
            prefix = "0";
        }

    // now we have a formatted string for output, to be possibly padded
    outstr((char*)p, prefix);
    return *this;
}
// format and insert an unsigned long
ostream _FAR & ostream::operator<< (unsigned long ul)
{
    char buf[MaxCharsInLong];
    char *prefix = 0;       // displayed numeric prefix string
    char *p;


    if( flags() & ios::hex )
        {
        int upper = (flags() & ios::uppercase) != 0;
        p = tohex(buf + MaxCharsInLong - 1, ul, upper);

        // compute any base prefix
        if( flags() & ios::showbase )
            prefix = upper ? "0X" : "0x";
        }
    else if( flags() & ios::oct )
        {
        p = tooct(buf + MaxCharsInLong - 1, ul);

        // compute any base prefix
        if( flags() & ios::showbase )
            prefix = "0";
        }
    else
        {
        p = todec(buf + MaxCharsInLong - 1, ul);

        // compute any sign prefix
        if( ul  &&  (flags() & ios::showpos) )
            prefix = "+";
        }

    // now we have a formatted string for output, to be possibly padded
    outstr((char*)p, prefix);
    return *this;
}
Example #4
0
void
zpcgetuint64(struct zpctoken *token, const char *str, char **retstr)
{
    char     *ptr = (char *)str;
    uint64_t  u64 = 0;

    if (*ptr == '0') {
        ptr++;
        if (*ptr == 'x' || *ptr == 'X') {
            /* hexadecimal value */
            ptr++;
            while (isxdigit(*ptr)) {
                u64 <<= 4;
                u64 += tohex(*ptr);
                ptr++;
            }
            token->radix = 16;
            token->data.ui64.u64 = u64;
        } else if (*ptr == 'b' || *ptr == 'B') {
            /* binary value */
            ptr++;
            while (isxdigit(*ptr)) {
                u64 <<= 1;
                u64 += tobin(*ptr);
                ptr++;
            }
            token->radix = 2;
            token->data.ui64.u64 = u64;
        } else if (isdigit(ptr[1])){
            /* octal value */
            while (isxdigit(*ptr)) {
                u64 <<= 3;
                u64 += tooct(*ptr);
                ptr++;
            }
            token->radix = 8;
            token->data.ui64.u64 = u64;
        }
    } else if (isdigit(*ptr)) {
        /* decimal value */
        while (isdigit(*ptr)) {
            u64 *= 10;
            u64 += todec(*ptr);
            ptr++;
        }
        token->radix = 10;
        token->data.ui64.u64 = u64;
#if 0
    } else if (isxdigit(*ptr)) {
        while (isxdigit(*ptr)) {
            u64 <<= 4;
            u64 += tohex(*ptr);
            ptr++;
        }
        token->data.ui64.u64 = u64;
#endif
    }
    if (*ptr == 'u' || *ptr == 'U' || *ptr == ',') {
        ptr++;
    }
    token->type = ZPCUINT64;
    *retstr = (char *)ptr;

    return;
}