NO

Author Topic: char to char array  (Read 2906 times)

manichandra

  • Guest
char to char array
« on: February 27, 2012, 08:59:31 PM »
is there any possibility to convert a char to a char array
example :
here is char a ='163';
char data[16];
i want to input the value of 'a' to 'data' as
data[0]='1'
data[1]='6'
data[2]='3'
data[3]='\0'
« Last Edit: February 27, 2012, 09:01:33 PM by manichandra »

CommonTater

  • Guest
Re: char to char array
« Reply #1 on: February 27, 2012, 09:31:12 PM »
unsigned char a = 163 is valid ...
char a = '163' is not.
 
Look up sprintf() in your help file...
« Last Edit: February 27, 2012, 09:33:01 PM by CommonTater »

manichandra

  • Guest
Re: char to char array
« Reply #2 on: February 27, 2012, 10:18:17 PM »
sorry, i just tried to highlight the value. thats not the code.

« Last Edit: February 27, 2012, 10:45:10 PM by manichandra »

CommonTater

  • Guest
Re: char to char array
« Reply #3 on: February 28, 2012, 12:18:55 AM »
sorry, i just tried to highlight the value. thats not the code.

If you were using pelles C, you could look up the sprintf() function in your help file and see how to use it.

nancy

  • Guest
Re: char to char array
« Reply #4 on: February 28, 2012, 10:33:04 AM »
You can do this by yourself:

Code: [Select]
unsigned char a = (unsigned char)'\xa3'; //163
char data[4];
int value, pos = 0;

for (int i = 100; i > 0; i /= 10)
{
value = a / i;
a -= i * value;
data[pos++] = value + 48;
}
data[pos] = '\0';

This is very simple C code. If you do not understand this, you should take your C book and try to explore every bit of this. Just to copy the code will teach you nothing.
« Last Edit: February 28, 2012, 10:40:05 AM by nancy »