I need a translate (in both directions) all primitive types, into char[] (will be stored in string)
I understand how to manipulate integral types with bits and I cant just cut them down and shift them, but float and double don't work with this manipulation. So does anyone know how I can create a perfect bit copy of float and double?
Does anyone know whether memcpy(b2, sizeof(b2), b1, sizeof(b2)); would work? To create an exact copy of the bits stored in a float or double b1 and store it in a character array b2 ?
int i = 0xFCED03A4;
char * ch = (char*)&i;
int ch_size = sizeof(i);
// can access from ch[0] to ch[ch_size-1]
This isn't going to be recommended by any standard rules, but it works.
To switch to float or double, just change from int to float or double, the code will still keep working.
This did come to mind but with using the reinterpret_cast to check for errors, however I thought that the compiler might complain, or that if this is allowed that maybe Windows would have rules about its memory handling and prevent this from working during runtime
I don't think there's anything that avoids this to be good on known systems like Windows.
Windows itself likes to cast pointers to other types (see Handles like HWND, HMODULE itself is a pointer to IMAGE_DOS_HEADER).
Also I think it should be reinterpret_cast.
dynamic_cast is for checking compatibility between virtual classes.