itoa銆乴toa

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了itoa銆乴toa相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/--' title='--'>--   else   long   string   int   ret   tmp   str   code   

#include <stdlib.h>
    
/*鏁村舰杞瓧绗﹀瀷*/
char * itoa(int value, char *string, int radix)
{
    char tmp[33];
    char *tp = tmp;
    int i;
    unsigned v;
    int sign;
    char *sp;

    if (radix > 36 || radix <= 1)
    {
        return 0;
    }

    sign = (radix == 10 && value < 0);
    if (sign)
        v = -value;
    else
        v = (unsigned)value;

    while (v || tp == tmp)
    {
        i = v % radix;
        v = v / radix;
        if (i < 10)
            *tp++ = i + '0';
        else
            *tp++ = i + 'a' - 10;
    }

    if (string == 0)
        string = (char *)malloc((tp - tmp) + sign + 1);
    sp = string;

    if (sign)
        *sp++ = '-';
    while (tp > tmp)
        *sp++ = *--tp;
    *sp = 0;
    return string;
}

/*闀挎暣褰㈣浆瀛楃鍨?/
char *ltoa(long value, char *string, int radix)
{
    char tmp[33];
    char *tp = tmp;
    long i;
    unsigned long v;
    int sign;
    char *sp;

    if (radix > 36 || radix <= 1)
    {
        return 0;
    }

    sign = (radix == 10 && value < 0);
    if (sign)
        v = -value;
    else
        v = (unsigned long)value;

    while (v || tp == tmp)
    {
        i = v % radix;
        v = v / radix;
        if (i < 10)
            *tp++ = i + '0';
        else
            *tp++ = i + 'a' - 10;
    }

    if (string == 0)
        string = (char *)malloc((tp - tmp) + sign + 1);
    sp = string;

    if (sign)
        *sp++ = '-';
    while (tp > tmp)
        *sp++ = *--tp;
    *sp = 0;
    return string;
}

/*鏃犵鍙烽暱鏁村舰杞瓧绗﹀瀷*/
char *_ultoa(unsigned long value, char *string, int radix)
{
    char tmp[33];
    char *tp = tmp;
    long i;
    unsigned long v = value;
    char *sp;

    if (radix > 36 || radix <= 1)
    {
        return 0;
    }

    while (v || tp == tmp)
    {
        i = v % radix;
        v = v / radix;
        if (i < 10)
            *tp++ = i + '0';
        else
            *tp++ = i + 'a' - 10;
    }
    if (string == 0)
        string = (char *)malloc((tp - tmp) + 1);
    sp = string;

    while (tp > tmp)
        *sp++ = *--tp;
    *sp = 0;
    return string;
}

以上是关于itoa銆乴toa的主要内容,如果未能解决你的问题,请参考以下文章

更改片段后如何取消显示堆叠的吐司?

_itoa_s 函数在 Android Studio 本机 C++ 上显示错误

用jq实现toas 效果

基于到达时间(TOA)的室内定位(/无线传感器网络定位)——极大似然估计ML

基于到达时间(TOA)的室内定位(/无线传感器网络定位)——极大似然估计ML

_itoa 和 itoa 有啥区别?