6 ms·
No, sorry, you are wrong. If you are interested in the actual bits, char* is the way to go.
by pif 3y ago
No, sorry, you are wrong. If you are interested in the actual bits, char* is the way to go.
- tialaramex 3y agoThat's bytes, if you want bytes you need to specify what order you think the bytes are in, Rust's to_be_bytes, to_ne_bytes and to_le_bytes gives you bytes (in Big, Network, or Little endian)
- pif 3y agoI'm not sure that I understand your point. Actually, I wonder if you do ;-)
- tialaramex 3y agoRust provides methods to get your f32 (a 32-bit IEEE floating point number) or f64 (the 64-bit one) as, appropriately, a u32 or u64. Rust calls these methods to_bits because that's what you get. Conveniently Rust doesn't need to care about byte order for these operations because no extant computer makes a different choice for its integer and float representations. Good. You insisted you want char *, by which I understood you meant bytes. Rust can do that too, but now you need to care about byte order. Seeing another answer in this sub-thread I realise you actually meant you wanted a string which is doubly hilarious in a C++ thread. The char * is a pointer to a byte, C++ has actual strings (and, these days, even a passable fat pointer style string reference called std::string_view) and they're different.
- anonymoushn 3y agoIt seems reasonable to pass around 32 bits in a u32 or 64 bits in a u64 rather than using arrays.
- pif 3y agoIt is reasonable, but you must pay attention not to incur into undefined behaviour (which the post is about). An array of chars is the way to go for the C++ committee.