0

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]).

1
  • Can you clarify your question are you trying to convert 10 to a string "10" and then add it to your char array or put the binary representation in you array of bytes ? Commented Oct 28, 2019 at 0:08

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

3 Comments

That's what I was looking for. Could it be implemented with memcpy also?
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.
@Stelios if this is the right answer for you, please consider rephrasing you question to match the answer for future readers. I will then delete my answer below.
-1

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 as uint16_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

In this example you make assumptions about the size of int to be 32 bit.
You can do this by casting your 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."
(cont) Just Google SIGBUS ARM
I just answered what the question was asking for: "Is there any way I can put an integer into a string of known size and if yes how can I read it?" Neither does the author say a single word about converting the integer to ASCII, nor does he state anything about sizeof(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.
Write a comment about the interpenetration of the question and address the issues pointed out then this is good reference for someone in the future who have another interpretation of the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.