@jorz I'm not looking to change int into anything, I'm looking to change an array of small chars into one big int.
@Zhuge I just tried it but it's giving unexpected results. For example it gave 1128415488 with my "ABCD" example. I also thought I might be able to do it as individual characters and deal with the conversion from each so I tried
1 2 3 4 5 6 7 8
|
string a = "ABCD";
int c;
c=0;
for (int x=0; x<4; x++)
{
c+= (int(a.at(x))*pow(256,x));
}
cout << c <<endl;
|
But that gave unexpectedly large results too.
int("A") gives the expected value of 65 though.
Edit: I am right in my thinking that char 'A' is the exact same as unsigned char 65 and they're just interpreted differently, right?
Second edit: Nevermind. My previous code above works, it just has the characters put into the integer in the opposite order I had done in my example. It works fine though. I don't know why the union doesn't work but it gave a different answer that I can't seem to get from any combination of the letters
final edit: never mind. 'm a moron. When I set up the union I used positions 1,2,3,and4 in the char[] rather than 0,1,2, and 3. You were right, a union works fine for that.
everything's working as expected. I still don't get why you can't cast an array of known size into an integer though