Store an Int in a Char Array

Store an integer in a char array

cArray[6] = (char) 0; // WHY DOES THIS NOT WORK???
printf("%c\n", cArray[6]);

This code attempts to print a character with the encoding 0; assuming ASCII, nothing will get displayed because there is no printable character associated with that code.

If you intend to store the ASCII code for the character '0' and print it, then you need to write

cArray[6] = 48;               // same as writing cArray[6] = '0' (ASCII)
printf( "%c\n", cArray[6] );

This will print 0 to your console.

If, on the other hand, you want to store any arbitrary integer value1 to cArray[6] and display that value, then you need to use the %d conversion specifier:

cArray[6];
printf( "%d\n", cArray[6] );


1. That is, any integer that fits into the range of char, anyway

Store an integer in char array in C

You did store the integer in the character, it's just that %c converts a character to its ASCII value. All ASCII values below 31 are non-printable.

If you run

printf("b = %d and i = %d\n", (int)b, i);

it will print 15.

If you want a representation of i as a string:

char buf[12]; //Maximum number of digits in i, plus one for the terminating null
snprintf(buf, 12, "%d", i);

This will store a string representation of i in buf.

Store int inside char array in C

I have tried another way and i got a solution. I am posting it as a help for others.

char mem[50];
int* size = (int*)&mem[0];
*size = 1234;

char* isFree = (char*)&mem[4];
*isFree = 'f';

char *t = (char *)&mem[4];

printf("%d\n",*(int*)&mem[0]);
printf("%c\n",*(char*)&mem[4]);

I assigned a pointer to a pointer. I worked. Thank you all for answering.

Store an int in a char array?

Not the most optimal way, but is endian safe.


int har = 0x01010101;
char a[4];
a[0] = har & 0xff;
a[1] = (har>>8) & 0xff;
a[2] = (har>>16) & 0xff;
a[3] = (har>>24) & 0xff;

Storing multiple integers in a char array

You may want to use snprintf.

char buf[32];
snprintf(buf, 32, "%d %d", input.Z_pos_Camera_Temperature, input.Z_neg_Camera_Temperature);

Storing different data types in a char array

I guess the intent was to do something like this.

(of course we should check that no buffer overflow occurs)

#include <string.h>
#include <stdio.h>

int
main(void)
{
char something[14];

if(1) // store to something
{
int a=1234;
short b=5678;
double c=1234.5678;
int offset=0;
memcpy(something+offset, &a, sizeof(a));
offset+=(int)sizeof(a);
memcpy(something+offset, &b, sizeof(b));
offset+=(int)sizeof(b);
memcpy(something+offset, &c, sizeof(c));
offset+=(int)sizeof(c);
}

if(1) // load from something
{
int a;
short b;
double c;
int offset=0;
memcpy(&a, something+offset, sizeof(a));
offset+=(int)sizeof(a);
memcpy(&b, something+offset, sizeof(b));
offset+=(int)sizeof(b);
memcpy(&c, something+offset, sizeof(c));
offset+=(int)sizeof(c);
printf("%d %hd %g\n", a, b, c);
}

return 0;
}

Note that memcpy() is used here in order to be independent
from any alignment consideration.

If we assume that on a specific platform a, b and c have
respectively a 4-bytes, 2-bytes and 8-bytes size and alignment,
the corresponding bytes accessed inside the array thanks to
memcpy() could have any other alignment without any problem
(here they are simply packed).

              offset  0       4   6               14
bytes in the array [a a a a|b b|c c c c c c c c]

How to store an int in a char * that is assigned by malloc

You can copy into the buffer pointed to by c:

memcpy(c, &value, sizeof(value));

If you want to write another value following that, you can add offset to c:

memcpy(c + sizeof(value), &value2, sizeof(value2));  // adds value2 at an offset right after value

To read the value, you can copy it into a different variable:

int readVal;
memcpy(&readVal, c, sizeof(readVal));

How to store an integer variable as an input in character array in Java?

    String str = "aaabbcc";
char c;
ArrayList<String>count=new ArrayList();

for(int i = 0; i < str.length(); i++){
if(!count.contains(str.charAt(i)+"")){
count.add(str.charAt(i)+"");
}
}

int [] dd = new int [count.size()];

    for (int i = 0; i < str.length(); i++) {
c=str.charAt(i);
for(int j=0;j<count.size();j++){
if(count.get(j).equals(c+"")){
dd[j]++;
}
}
}

String newS="";
for(int i=0;i<count.size();i++){
newS+=count.get(i)+dd[i];
}

str = newS;

    System.out.println(str);


Related Topics



Leave a reply



Submit