Atoi and ITOA implementation

Source: Internet
Author: User
/***
* Atox. C-atoi and atol Conversion
*
* Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
*
* Purpose:
* Converts a character string into an int or long.
*
**************************************** ***************************************/

# Include <cruntime. h>
# Include <stdlib. h>
# Include <ctype. h>

/***
* Long atol (char * nptr)-convert string to long
*
* Purpose:
* Converts ASCII string pointed to by nptr to binary.
* Overflow is not detected.
*
* Entry:
* Nptr = PTR to string to convert
*
* Exit:
* Return long int value of the string
*
* Exceptions:
* None-overflow is not detected.
*
**************************************** ***************************************/

Long _ cdecl atol (
Const char * nptr
)
{
Int C;/* current char */
Long total;/* current total */
Int sign;/* If ''-'', then negative, otherwise positive */

/* Skip whitespace */
While (isspace (INT) (unsigned char) * nptr ))
++ Nptr;

C = (INT) (unsigned char) * nptr ++;
Sign = C;/* save sign indication */
If (C = ''-'' | C = ''+ '')
C = (INT) (unsigned char) * nptr ++;/* Skip sign */

Total = 0;

While (isdigit (c )){
Total = 10 * Total + (c-'0');/* accumulate digit */
C = (INT) (unsigned char) * nptr ++;/* Get next char */
}

If (Sign = ''-'')
Return-total;
Else
Return total;/* return result, negated if necessary */
}

/***
* Int atoi (char * nptr)-convert string to long
*
* Purpose:
* Converts ASCII string pointed to by nptr to binary.
* Overflow is not detected. Because of this, we can just use
* Atol ().
*
* Entry:
* Nptr = PTR to string to convert
*
* Exit:
* Return int value of the string
*
* Exceptions:
* None-overflow is not detected.
*
**************************************** ***************************************/

Int _ cdecl atoi (
Const char * nptr
)
{
Return (INT) atol (nptr );
}

# Ifndef _ no_int64

_ Int64 _ cdecl _ atoi64 (
Const char * nptr
)
{
Int C;/* current char */
_ Int64 total;/* current total */
Int sign;/* If ''-'', then negative, otherwise positive */

/* Skip whitespace */
While (isspace (INT) (unsigned char) * nptr ))
++ Nptr;

C = (INT) (unsigned char) * nptr ++;
Sign = C;/* save sign indication */
If (C = ''-'' | C = ''+ '')
C = (INT) (unsigned char) * nptr ++;/* Skip sign */

Total = 0;

While (isdigit (c )){
Total = 10 * Total + (c-'0');/* accumulate digit */
C = (INT) (unsigned char) * nptr ++;/* Get next char */
}

If (Sign = ''-'')
Return-total;
Else
Return total;/* return result, negated if necessary */
}

# Endif/* _ no_int64 */

# Include <msvcrt/errno. h>
# Include <msvcrt/stdlib. h>
# Include <msvcrt/Internal/file. 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)
{
_ Set_errno (Edom );
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.