For both little endian and big endian?
What if they are in an array? Is it safe too?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This has nothing to do with Endianness.
As you can see here, char is almost certainly going to be 8 bit, so casting that to integer is always safe in every sense of the word.
Casting an int to a double, however, is not. The number 2^63 – 10, e.g., cannot be represented exactly in a 64 bit double. Read What Every Computer Scientist Should Know About Floating-Point Arithmetic. Here, a cast will lead to the number being truncated, and thus precision loss.
Fortunately, all 32 bit ints can be casted without loss, so if your ints 32 bits long, you’re good.