关闭

ultoa

683人阅读 评论(0) 收藏 举报

/*
 * convert unsigned long to ASCII
 * Copyright (C) 1998, 1999  Henry Spencer.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/lgpl.txt>.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
 * License for more details.
 *
 * RCSID $Id: ultoa.c,v 1.2 2004/02/22 06:50:42 mcr Exp $
 */
#include "internal.h"
#include "freeswan.h"

/*
 - ultoa - convert unsigned long to decimal ASCII
 */
size_t                          /* length required for full conversion */
ultoa(n, base, dst, dstlen)
unsigned long n;
int base;
char *dst;                      /* need not be valid if dstlen is 0 */
size_t dstlen;
{
        char buf[3*sizeof(unsigned long) + 1];
        char *bufend = buf + sizeof(buf);
        size_t len;
        char *p;
        static char hex[] = "0123456789abcdef";

        p = bufend;
        *--p = '/0';
        if (base == 10) {
                do {
                        *--p = n%10 + '0';
                        n /= 10;
                } while (n != 0);
        } else if (base == 16) {
                do {
                        *--p = hex[n&0xf];
                        n >>= 4;
                } while (n != 0);
                *--p = 'x';
                *--p = '0';
        } else if (base == 8) {
                do {
                        *--p = (n&07) + '0';
                        n >>= 3;
                } while (n != 0);
                *--p = '0';
        } else
                *--p = '?';

        len = bufend - p;

        if (dstlen > 0) {
                if (len > dstlen)
                        *(p + dstlen - 1) = '/0';
                strcpy(dst, p);
        }
        return len;
}

0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10895次
    • 积分:222
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:1篇
    • 译文:2篇
    • 评论:3条
    文章分类
    最新评论