convert 4 char array or string to integer representation

I'm trying to cast a 4 character string to an integer that represents the characters. I do not want atoi as I don't want to take a string "1234" and make it integer 1234 but rather I want to take string "ABCD" and make it into integer 1094861636 (41424344 in hex which represents "A" with the first two digits B with the second two and so on) I tried static_cast<int> but that doesn't work. Why is this not possible, because isn't a char just a numeric type that gets interpreted as characters? A 4 char array string should be able to fit perfectly within an integer but I can't find any way to get it to work.

If anyone has any ideas please let me know. I'd love to hear them.
Perhaps something like this?
1
2
3
4
5
6
7
8
9
10
11
union {
    char char_data[4];
    int int_data;
} test;
// set the elements in the character array
test.char_data[0] = '1';
test.char_data[1] = '2';
test.char_data[2] = '3';
test.char_data[3] = '4';
// retrieve as an integer
std::cout<<test.int_data;

Note: I didn't actually test this.
what you are looking for is changing int to bcd search for that and you will find what you need.
@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
Last edited on
If you have for example character array "ABCD" then whether you will get 0x41424344 or 0x44434241 depends of the machine you use.

For example on Intel mashine you will get 0x44434241 if you will interprete the memory occupied by the array as a value of int type.

But you can use a character literal instead of acharacter array.

Consider the following example

1
2
3
4
5
6
7
8
9
10
char s[] = { 'A', 'B', 'C', 'D' };
int x;

x = *reinterpret_cast<int *>( s );

std::cout << std::hex << x << std::endl;

int y = 'ABCD';

std::cout << std::hex << y << std::endl;


In main machine I get

44434241
41424344


Last edited on
That has to do with the endianess of the system right?

And correct me if I'm wrong but while the union or de-referenced reinterpret cast (I guess that's how you do it. I knew it should be doable with some type of cast) are both dependant on system, which would make the results inconstant, parsing it one character at a time using
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,3-x));
    }
    cout << cout.hex << c <<endl;

Would be consistent, even if it is substantially more processor intensive, right?

I'm hoping that the code I use will be nearly completely portable, if not entirely. I don't think character literals are an option for me as they need to be able to be assembled and disassembled durring runtime. Character literals are constants only since they are int type (when longer than 1 character) right?
Last edited on
Topic archived. No new replies allowed.