C/Data Type/Int Convert

Материал из C\C++ эксперт
Перейти к: навигация, поиск

A function to return a string representation of an integer

<source lang="cpp"> /* Beginning C, Third Edition

By Ivor Horton
ISBN: 1-59059-253-0
Published: Apr 2004
Publisher: apress
  • /
  1. include <stdio.h>

/* Convert an integer to a string */ /* Caller must allocate string array */ /* Function returns the string to allow */ /* Use of the function in an expression. */ char* itoa(int n, char str[]) {

 int i = 0;               /* Loop counter              */
 int negative = 0;        /* Indicate negative integer */
 int length = 0;          /* Length of string          */
 int temp = 0;            /* Temporary storage         */

 if(negative = (n<0))     /* Is it negative?  */
   n = -n;                /* make it positive */
 /* Generate digit characters in reverse order */
 do
 {
   str[i++] = "0"+n%10;    /* Create a rightmost digit        */
   n /= 10;                /* Remove the digit                */              
 }while(n>0);              /* Go again if there"s more digits */
 if(negative)              /* If it was negative */
   str[i++] = "-";         /* Append minus       */
 str[i] = "\0";            /* Append terminator  */
 length = i;               /* Save the length    */
 /* Now reverse the string in place */
 /* by switching first and last,    */
 /* second and last but one, etc    */
 for(i = 0 ; i<length/2 ;i++)
 {
   temp = str[i];
   str[i] = str[length-i-1];
   str[length-i-1] = temp;
 }
 return str;                /* Return the string */

}

void main() {

  char str[15];              /* Stores string representation of integer */
  long testdata[] = { 30L, -98L, 0L, -1L, 999L, -12345L};  
  int i = 0;                /* Loop control variable                    */
  for (i = 0 ; i< sizeof testdata/sizeof(long) ; i++)
    printf("Integer value is %d, string is %s\n", testdata[i], itoa(testdata[i],str));

}


      </source>


A function to return a string representation of an integer with a given width

<source lang="cpp"> /* Beginning C, Third Edition

By Ivor Horton
ISBN: 1-59059-253-0
Published: Apr 2004
Publisher: apress
  • /
  1. include <stdio.h>

/* Convert an integer to a string with a fixed width. */ /* if the widh is too small, the minimum width is assumed. */ char* itoa(int n, char str[], int width) {

 int i = 0;               /* Loop counter              */
 int j = 0;               /* Loop counter              */
 int negative = 0;        /* Indicate negative integer */
 int length = 0;          /* Length of string          */
 int temp = 0;            /* Temporary storage         */

 if(negative = (n<0))     /* Is it negative?  */
   n = -n;                /* make it positive */
 /* Generate digit characters in reverse order */
 do
 {
   str[i++] = "0"+n%10;    /* Create a rightmost digit        */
   n /= 10;                /* Remove the digit                */              
 }while(n>0);              /* Go again if there"s more digits */
 if(negative)              /* If it was negative */
   str[i++] = "-";         /* Append minus       */
 str[i] = "\0";            /* Append terminator  */
 length = i;               /* Save the length    */
 /* Now reverse the string in place */
 /* by switching first and last,    */
 /* second and last but one, etc    */
 for(i = 0 ; i<length/2 ;i++)
 {
   temp = str[i];
   str[i] = str[length-i-1];
   str[length-i-1] = temp;
 }
 /* Shift the string to the right and insert blanks */
 if(width>length)
 {
   for(i=length, j = width ; i>= 0 ; i--, j--)
     str[j] = str[i];
   for(i = 0 ; i<width-length ; i++)
     str[i] = " ";
 }
 return str;                /* Return the string */

}

void main() {

  char str[15];              /* Stores string representation of integer */
  long testdata[] = { 30L, -98L, 0L, -1L, 999L, -12345L};  
  int i = 0;                /* Loop control variable                    */
  for (i = 0 ; i< sizeof testdata/sizeof(long) ; i++)
    printf("Integer value is %10d, string is %s\n", testdata[i], itoa(testdata[i],str, 14));

}


      </source>


Convert an integer to words

<source lang="cpp"> /* Beginning C, Third Edition

By Ivor Horton
ISBN: 1-59059-253-0
Published: Apr 2004
Publisher: apress
  • /

/* */

  1. include <stdio.h>
  2. include <string.h>

void main() {

 char *unit_words[] = {"zero", "one","two","three","four","five","six","seven","eight","nine"};
 char *teen_words[] = {"ten", "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
 char *ten_words[] = {"error", "error","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
 char hundred[] = " hundred";
 char and[] = " and ";
 char value_str[50] = "";
 int value = 0;                       /* Integer to be converted        */
 int digits[] = {0,0,0};            /* Stores digits of value entered */
 int i = 0;
 printf("Enter an integer less than 1000: ");
 scanf("%d",&value);
 if(value>=1000)
   value =999;
 else if(value<1)
   value = 1;
 while(value>0)
 {
   digits[i++] = value%10;
   value /= 10;
 }
 if(digits[2] > 0)
 {
   strcat(strcat(value_str,unit_words[digits[2]]), hundred);
   if(digits[1] >0 || digits[0] > 0)
     strcat(value_str, and);
 }
 if(digits[1] > 0)
 {
   if(digits[1] == 1)
     strcat(value_str,teen_words[digits[0]]);
   else
   {
     strcat(value_str,ten_words[digits[1]]);
     if(digits[0] > 0)
       strcat(strcat(value_str, " "), unit_words[digits[0]]);
   }
 }
 else
   if(digits[0] > 0)
     strcat(value_str, unit_words[digits[0]]);
 printf("\n%s\n", value_str);

}


      </source>


Convert integer to string: how to use itoa

<source lang="cpp">

  1. include <stdio.h>
  2. include <stdlib.h>

int main () {

 int i;
 char str[33];
 printf ("Enter a number: ");
 scanf ("%d",&i);
 
 itoa (i,str,10);
 printf ("decimal: %s\n", str);
 
 itoa (i, str, 16);
 printf ("hexadecimal: %s\n", str);
 
 itoa (i, str, 2);
 printf ("binary: %s\n", str);
 
 return 0;

}

      </source>