Is there any way I can put an integer into a string of known size and if yes how can I read it? For example an (int a=10) into a (char string[200]).
2 Answers
Is there any way I can put an integer into a string of known size and if yes how can I read it? For example an (
int a=10) into a (char string[200]).
Yes, there is use the string version of printf
snprinf(string,200,"%d",a);
It is also possible to generate the string with a loop with div and mod by 10 if you want to do above by hand. See another answer for an example of this https://stackoverflow.com/a/49615617/391691
To get your int back from the string sscanf, atoi or atol can be used
#include <stdio.h>
#include <stdlib.h>
void main(){
int a;
char string[200];
a = 10;
snprintf(string,200,"%d",a);
printf("As string:%s As int:%d\n",string,atoi(string));
}
The interpretation of this question is still not clear to me, this represents an alternative interpretation of the question compared to previous answers https://stackoverflow.com/a/58584291/391691
3 Comments
memcpy would represent the other interpretation of this question where you just store the raw data in the char array -- which will at this point not be a string.Is there any way I can put an integer into a string of known size and if yes how can I read it? For example an (int a=10) into a (char string[200]).
In this answer I work with the minimal question stated, which does not give details about any of the following:
- Representation / encoding of the integer in
string - Architecture this should run on
- The position where to insert the integer
If this question is about converting an integer to its ASCII representation the answer by @Simson is what you are looking for.
C allows you to "put"[1] an integer into any position of a c style string by casting the char array to an int*.
// string: the array (char*)
// pos: position in the array (size_t)
// to_insert: the value to insert (int)
(*(int*)(&string[pos])) = to_insert;
But be warned: This is a sledgehammer approach. You have to be very careful when doing this or your code might show undefined behavior on different platforms. For example you have to manually check and answer:
- Memory alignment: will I try to insert the integer at a position which is not addressable by the architecture?
- Endianness: is the way that I write this integer to the array actually what I want?
sizeof(int): does the integer still fit into the array? Maybe consider using any fixed length integer types such asuint16_t,uint32_t, ...- and possibly a few more
The following example shows the usage of the method discussed:
#include <stdio.h>
void insert_at_pos(char* arr, size_t pos, int to_insert){
(*(int*)(&arr[pos])) = to_insert;
}
int main(void) {
char string[200] = {0};
insert_at_pos(string, 3, 10);
//print char by char as hex
for (size_t i=0; i < sizeof(string)/sizeof(*string); ++i){
printf("%x ",(unsigned char)string[i]);
}
}
Running the example:
$ ./main
0 0 0 a 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[1]: And by "put" I understand write as-is.
5 Comments
char array string to an int*. No, you can not safely do that. (*(int*)(&string[pos])) = to_insert; violates the [strict aliasing rule](stackoverflow.com/questions/98650/… and may also invoke undefined behavior per 6.3.2.3 Pointers, paragraph 7: "A pointer to an object type may be converted to a pointer to a different object type. If the resulting pointer is not correctly aligned68) for the referenced type, the behavior is undefined."SIGBUS ARMsizeof(integer) or the position where to insert that int. I do know that my answer might cause undefined behavior on different systems but it tries to do exactly what the author is asking for.