For both little endian and big endian?
What if they are in an array? Is it safe too?
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.
Yes, as long as you are type casting regular C variables, it is safe.
You only need to worry about endian if you are altering or moving with the raw bytes that make up those variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With