ltoa

 I don't know who wrote it, but it looks nice.

/* Long to ASCII conversion routine - used by print, and those programs
 * which want to do low level formatted output without hauling in a great
 * deal of extraneous code.  This will convert a long value to an ascii
 * string in any radix from 2 - 16.
 * RETURNS - the number of characters in the converted buffer.
 */

static WCHAR digits[] = {
    L'0', L'1', L'2', L'3', L'4',
    L'5', L'6', L'7', L'8', L'9',
    L'a', L'b', L'c', L'd', L'e', L'f'
};

#define BITS_IN_LONG  (8*sizeof(long))

int
zltoa(
    long aval,
    register WCHAR *buf,
    int base
    )
{
    // if unsigned long wont work on your host, you will probably have
    // to use signed long and accept this as not working for negative
    // numbers.

    register unsigned long val;
    register WCHAR *p;
    WCHAR tbuf[BITS_IN_LONG];
    int size = 0;

    p = tbuf;
    *p++ = L'\0';

    if (aval < 0 && base == 10) {
        *buf++ = L'-';
        val = -aval;
        size++;
    } else {
        val = aval;
    }

    do {
        *p++ = digits[val % base];
    } while (val /= base);

    while ((*buf++ = *--p) != 0) {
        ++size;
    }

    return(size);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值