How to convert int to char

Hey,
I am tying to convert an int to a char. Below is an example of the code that I have but I am getting an error('=':left operand must be l-value). I am not seeing a problem with the code.
1
2
3
4
5
6

int num = 5;
char temp[2];
char final[2];
itoa(num, temp, 10);
m_pRes->final = temp;


Any help on this will be greatly appreciated!!
Thanks In Advance
You may not assign one array to another. Arrays have no the assignment operator.
a small test in converting ints to chars

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <iostream>

using std::cout;

int main()
{
    int a = 339;

    char b = a;

    char c = (char)a;

    int d = (int)b;

    int e = a & 0xff;

    cout << a << " " << b << " " << c << " " << d << " " << e << std::endl;
}


from the output we can see that the way that casting (char)num works is the same way as logicaly AND the int with only the first 8 bits.

what I dont understand in your code is m_pRes and itoa, what you need em for? just use casting
Unless you're trying to do something like this:?

1
2
3
4
5
6
7
int digi = 5;
std::string buf = "";
char str[20];
buf += itoa(digi, str, 10);
printf("%s \r\n", buf)

// Output:  5 
itoa is not defined in C++ or many compilers. Using sprintf or using the string stream is more advised.

Topic archived. No new replies allowed.