c++ - store byte[4] in an int -



c++ - store byte[4] in an int -

i want take byte array 4 bytes in it, , store in int.

for illustration (non-working code):

unsigned char _bytes[4]; int * combine; _bytes[0] = 1; _bytes[1] = 1; _bytes[2] = 1; _bytes[3] = 1; combine = &_bytes[0];

i not want utilize bit shifting set bytes in int, point @ bytes memory , utilize them int if possible.

in standard c++ it's not possible reliably. strict aliasing rule says when read through look of type int, must designate int object (or const int etc.) otherwise causes undefined behaviour.

however can opposite: declare int , fill in bytes:

int combine; unsigned char *bytes = reinterpret_cast<unsigned char *>(&combine); bytes[0] = 1; bytes[1] = 1; bytes[2] = 1; bytes[3] = 1; std::cout << combine << std::endl;

of course, which value out of depends on how scheme represents integers. if want code utilize same mapping on different systems can't utilize memory aliasing; you'd have utilize equation instead.

c++ int byte data-type-conversion

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -