Problems converting unsigned char array to char array.

I'm trying to convert unsigned char array to simple char array.
However, it throws a strange error:
error: incompatible types in assignment of 'char*' to 'char [4]'
.

Here's my first code:

1
2
3
4
unsigned char uinfo[4] = { 0,1,16,32 };
char info[4];

info=(char*)uinfo;


it throwed this error. Then i tried this:

 
info= reinterpret_cast<char*>(uinfo);


But it throwed the same error! What's wrong?!
You cannot assign to arrays like that. Arrays and pointers are two entirely different things.
Last edited on
you could just point to the other array, try it with char* info=(char*)(&uinfo)


1
2
3
4
5
6
7
8
unsigned char uinfo[4] = { 'a','b','c','d' };

char* info;
info = (char*)(&uinfo); // point to the address of uinfo

info[3] = 0; // make sure the cstring is 0-terminated

std::cout << info << std::endl;


but i don't know what u try to do...
And I'd recommend you: stop doing things like this unless you know exactly what you are doing^^
Last edited on
i'm just converting numbers to unsigned char (byte) array, and trying to write those bytes to file as chars
It works now! I created a simple function for converting.

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;

void uncharToChar(unsigned char ar1[], char ar2[], int hm)
{
    for(int i=0; i<hm; i++)
    {
        ar2[i]=static_cast<char>(ar1[i]);
    }
}

int main()
{
    unsigned char uinfo[5] = { 0,16,32,64,255 };
    char info[5];

    uncharToChar(uinfo,info,5);

    for(int i=0; i<5; i++)
    {
        cout<<"Byte no."<<i<<":  "<<(int)info[i]<<" ";
        cout<<(int)(static_cast<unsigned char>(info[i]))<<endl;
    }

    return 0;
}
hakeris1010 wrote:
i'm just converting numbers to unsigned char (byte) array, and trying to write those bytes to file as chars
Keep in mind that in C++, unsigned char, signed char, and char are all distinct types. The standard stream libraries work with the char datatype, not the signed or unsigned versions. You should avoid converting between them as much as possible.
Topic archived. No new replies allowed.