ITOA () function, 10 hexadecimal conversion to (2 ~ 36) hexadecimal

Source: Internet
Author: User

 

Let's take a look at the ITOA () function description:

Function: converts an integer to a string.

Usage: char * ITOA (INT value, char * string, int Radix );

Description: ITOA is the abbreviation of the English integer to array (converting the int integer into a string and saving the value in the array string.

Parameter: value: the integer to be converted.

Radix: indicates the base number, that is, converting value to the base number of Radix,Range: 2-36For example, 10 indicates the hexadecimal notation, and 16 indicates the hexadecimal notation.

* String: Save the converted string.

Return Value: char *: indicates the generated string, which is the same as * string.

Note: The header file of this function is "stdlib. H" (included in iostream)
 

Remember: ITOA is not a standard C function, it is unique to Windows, if you want to write cross-platformProgram, Use sprintf.
It is extended on the Windows platform. The standard library contains sprintf, which is more powerful than this one. Its usage is similar to that of printf.

 

Although ITOA may not be available, we can write our own ITOA () functions.Source code(Source Network ):

Char * my_itoa (INT num, char * STR, int Radix) <br/>{< br/> const char table [] = "0123456789 abcdefghijklmnopqrstuvwxyz "; <br/> char * PTR = STR; <br/> bool negative = false; <br/> If (num = 0) <br/> {<br/> // num = 0 <br/> * PTR ++ = '0'; <br/> * PTR = '/0 '; <br/> // don't forget the end of the string is '/0 '!!!!!!!!! <Br/> return STR; <br/>}< br/> If (Num <0) <br/>{< br/> // If num is negative, the add'-'and change num to positive <br/> * PTR ++ ='-'; <br/> num * =-1; <br/> negative = true; <br/>}< br/> while (Num) <br/>{< br/> * PTR ++ = table [num % Radix]; <br/> num/= Radix; <br/>}< br/> * PTR = '/0'; <br/> // If num is negative, the add'-'and change num to positive <br/> // in the below, we have to converse The stri Ng <br/> char * Start = (negative? STR + 1: Str); <br/> // now start points the head of the string <br/> PTR --; <br/> // now PRT points the end of the string <br/> while (start <PTR) <br/>{< br/> char temp = * start; <br/> * Start = * PTR; <br/> * PTR = temp; <br/> Start ++; <br/> PTR --; <br/>}< br/> return STR; <br/>}

 

The program test is as follows:

# Include <iostream> <br/> using namespace STD; <br/> int main () <br/>{< br/> int A = 15; <br/> char STR [1, 100]; <br/> my_itoa (A, STR, 15); <br/> cout <STR <Endl; <br/> return 0; <br/>}

 

For the number that has been converted to the specified hexadecimal value, which is a string, we can convert it to an integer (2 ~ 10 hexadecimal), for example:

Int main () <br/>{< br/> int A = 15; <br/> char STR [100]; <br/> int n = atoi (my_itoa (A, STR, 2); <br/> cout <n <Endl; <br/> return 0; <br/>}

Note: atoi () is a function in the standard library, which is included in the C/C ++ language reference function, but not in ITOA (). We 'd better implement the ITOA () function by ourselves.

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.