Typecasting char array to int ( not Converting )

Hi all

Im looking for the correct syntax to typecast a character array as an int.
So i would be able to do something like this:

1
2
3
char ch[4];
//...Read characters from file into ch or something, etc...
int i = ( int ) &ch; 


or

1
2
3
4
char ch[4];
//...Read characters from file into ch or something, etc...
int i;
strncpy ( ( char* ) &i, ch, 4 )


I want all the 4 characters ( or bytes ) of ch to be classed as an int. So we are taking the 4 bytes of the character array and placing them in 4 bytes of the integer? How would i go about this?
Correct me if I am wrong. But you cannot do aggregate operations on arrays after they have been initialized?

Edit Spelling
Last edited on
I don't ever like reading anything more than a byte at a time from a binary file because you are subject to system endianness.

If you want to read a 32-bit value from a binary file... I just recently made a post where I gave a guy routines for both little and big endian files.

See this post:
http://www.cplusplus.com/forum/beginner/134169/#msg717778
Last edited on
Topic archived. No new replies allowed.