mercredi 9 octobre 2019

Most efficient way to transform 4 bytes ascii sequence codified within an int32 to a std::string

I am looking to a non-obvious way to transform 4 ascii chars encoded within a normal 32 bits integer as a std::string.

As example, the string "0123" is encoded in a 32 bits integers as:

int value = 0x30313233

or just the same,

int value = 808530483

I am looking for a way to transfor it back to a string containing its ascci representation "0123".

Of course it is really easy to solve with a while loop and shift operator, getting 8 bits at every iteration and static_casting to char, but I am interested in some not obvious solution involving std::ostringstream or similar.

Thank you very much in advance.

NOTE: I don't include any code due to the simplicity of the problem, if anyone is interested, I can write down the obvious while way.

Aucun commentaire:

Enregistrer un commentaire