Convert integer to array

I am working on a C program and I am running into a small problem. I don't know how to convert an integer (e.g. 2007) to a char array. Is there a function in C libraries for this?

To clarify, I would like to take 2007 and store it in some array char [4] = {'2', '0', '0', '7', '\ 0'};

I thought something like sprintf, but I'm not sure. In any case, any help / tips would be appreciated.

Thanks Michael

+2
c
source share
8 answers

You can do this with sprintf or more safely snprintf .

Here is a contrived example:

 #include <stdio.h> #define MAX_LEN 5 char str[MAX_LEN]; snprintf(str, MAX_LEN, "%d", 2007); 
+12
source share

Use snprintf() and don't forget to allocate enough space to store numbers up to 2 ^ (sizeof (int) * CHAR_BIT). On a 64-bit machine, this will be a 20-digit character, plus 1 for the NULL terminator.

 #include <stdio.h> #define MAX_DIGITS 20 int n = 2007; char str[MAX_DIGITS+1]; snprintf(str, MAX_DIGITS+1, "%d", n); 
+4
source share

As others have said, you should look at sprintf() or snprintf() . Assuming you are trying to convert an integral type T to such an array, an interesting bit is to determine the buffer size for the array.

First, the number of digits in decimal notation with numbers is & llloor; log 10 n + 1 & rfloor ;. The maximum possible value of an unsigned integer type T can be represented in binary bits nbits = CHAR_BIT*sizeof(T) , which requires <log 10 2 nbits & rfloor; +1 decimal digits.

log 10 2 nbits = nbits x times; log 10 2 = nbits x times; log (2) / log (10).

28/93 - a very good rational approximation of 1 log(2)/log(10) (0.30107526881720431 versus 0.30102999566398114).

So, using the above, we get our expression for the number of digits:

 CHAR_BIT * sizeof(T) * 28 / 93 + 1 

For signed numbers, we need to add 1 more for the - sign, and we need to add 1 for the final 0 . So we get:

 #include <limits.h> /* Figure out the maximum number of characters including the terminating 0 to represent the numbers in integral type T */ #define SZ(T) (CHAR_BIT * sizeof(T) * 28 / 93 + 3) 

So we can do:

 char array[SZ(int)]; sprintf(array, "%d", n); 

And we are sure that array has enough space. We do not need to worry about the combination of snprintf() or malloc() / realloc() and free() .

Here is the complete program using the above:

 #include <stdio.h> #include <limits.h> /* Figure out the maximum number of characters including the terminating 0 to represent the numbers in integral type T */ #define SZ(T) (CHAR_BIT * sizeof(T) * 28 / 93 + 3) #define PRINT(x) do \ { \ printf("%s: %lu\n", #x, (unsigned long)SZ(x)); \ } while (0) int main(void) { PRINT(int); PRINT(long); PRINT(size_t); return 0; } 

1 or enough for this purpose.

+3
source share
 #include <stdio.h> char array[5]; sprintf(array, "%d", 2007); 

... done.

Please note that sprintf is not safe overflow, so if you have 5 or more digits you will have a problem. Note also that the converted number will be followed by a trailing \0 .

+2
source share

Use snprintf :

 // value is int; // buf is char *; // length is space available in buf snprintf(buf, length, "%d", value) 

snprintf has the advantage of being standard and gives you more flexibility regarding formatting, and is also safe.

You can also use itoa , but warn that it is not part of the standard. However, in most implementations there are.

Using:

 // value is int; // buf is char *; itoa(value, buf, 10); 

An interesting question: how much space do you allocate for buf ? Note the following. If sizeof(int) bytes per int and eight bits per byte, the maximum value is approximately 2^(CHAR_BIT * sizeof(int) - 1) ( -1 for the sign bit). Therefore we need storage space

 floor(log_10(2^(CHAR_BIT * sizeof(int) - 1)) + 1 

numbers. But do not forget the sign and the zero limiter! So the maximum integer length here

 floor(log_10(2^(CHAR_BIT * sizeof(int) - 1)) + 3. 

Of course, this can mean wasting space if our values ​​are small. To find out how much space is required for a particular value:

 floor(log_10(abs(value))) + 1 + (value < 0 ? 1 : 0) + 1 
+1
source share

The classic way is itoa . Or you can use snprintf to get more control.

+1
source share

There is a non-standard, but well supported, as I support it, itoa function - the opposite of atoi .

Example:

 char *a = malloc(10 * sizeof(char)); itoa(2007, a, 2007); 

sprintf also works:

 char *a = malloc(10 * sizeof(char)); sprintf(a, "%d", 2007); 
+1
source share
 int n = 2007; char a[100]; sprintf( a, "%d", n ); 
+1
source share

Source: https://habr.com/ru/post/650031/


All Articles