Char * ITOA (INT value, char * string, int Radix) converts an integer to a string

Source: Internet
Author: User
Char * ITOA (INT value, char * string, int Radix); int value converted integer, char * string converted character array, int Radix converted hexadecimal number, for example, 2, 8, 10, 16
Hexadecimal header files: <stdlib. h>

ITOA operation and usage

Program example:

# Include <stdlib. h> # include <stdio. h> int main () {int number = 123456; char string [25]; ITOA (number, String, 10 ); printf ("integer = % d string = % s \ n", number, string); Return 0;}/* source code for implementing the ITOA function */char * myitoa (INT num, char * STR, int Radix ){

ITOA Flowchart

/* Index table */

Char index [] = "0123456789 abcdefghijklmnopqrstuvwxyz"; unsigned unum;/* intermediate variable * int I = 0, J, K; /* determine the unum value */If (Radix = 10 & num <0)/* decimal negative number */{unum = (unsigned)-num; STR [I ++] = '-';} else unum = (unsigned) num;/* Other cases * // * Reverse Order */do

C Language Program

{

STR [I ++] = index [unum % (unsigned) Radix]; unum/= Radix;} while (unum); STR [I] = '\ 0 '; /* convert */If (STR [0] = '-') k = 1;/* decimal negative number */else K = 0; char temp; for (j = K; j <= (i-k-1)/2; j ++) {temp = STR [J]; STR [J] = STR [i-j-1]; STR [i-j-1] = temp;} return STR;} The third parameter of ITOA is used to convert numbers into different hexadecimal values. For example: # include <stdlib. h> # include <stdio. h> int main (void) {int number = 12345; char string [25]; ITOA (number, String, 10 ); // convert printf in decimal format ("integer = % d string = % s \ n", number, string); ITOA (number, String, 16 ); // convert printf in hexadecimal notation ("integer = % d string = % s \ n", number, string); Return 0;}. output result: integer = 12345 string = 12345 -- the decimal representation of 12345 is 12345 integer = 12345 string = 3039 -- The hexadecimal representation of 12345 is 0x3039, but note that ITOA is not A standard C function exclusive to Windows. If you want to write cross-platform programs, use sprintf. In a few hexadecimal notation. :) The msdn example is example/* ITOA. c: This program converts Integers of various * sizes to strings in various radixes. */# include <stdlib. h> # include <stdio. h> void main (void) {char buffer [20]; int I = 3445; long l =-344115l; unsigned long ul = 1234567890ul; _ ITOA (I, buffer, 10); printf ("string of integer % d (Radix 10): % s \ n", I, buffer); _ ITOA (I, buffer, 16 ); printf ("string of integer % d (Radix 16): 0x % s \ n", I, buffer); _ ITOA (I, buffer, 2 ); printf ("string of integer % d (Radix 2): % s \ n", I, buffer); _ ltoa (L, buffer, 16 ); printf ("string of long int % LD (Radix 16): 0x % s \ n", l, buffer); _ ultoa (UL, buffer, 16 ); printf ("string of unsigned long % lu (Radix 16): 0x % s \ n", UL, buffer);} outputstring of integer 3445 (Radix 10 ): 3445 string of integer 3445 (Radix 16): 0xd75string of integer 3445 (Radix 2): 110101110101 string of long int-344115 (Radix 16 ): 0 xfffabfcdstring of unsigned long 1234567890 (Radix 16): 0x499602d2 specifies the base number of the base number to be converted. The value seems to be between 1 and 36. This is not a function in the C standard library, it is extended on the Windows platform. The standard library has sprintf, which is more powerful than this one. Its usage is similar to that of printf: Char STR [255]; sprintf (STR, "% x", 100 ); // convert 100 to a hexadecimal 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.