I have an array of unsigned char that represents a 128 bit number in network byte order. How would I go about converting this to host byte order efficiently (in this case x86_64)?
There don't seem to be any macros available in endian.h and my attempt at converting the upper 64 bits and lower 64 bits independently didn't work. The only method I've found that definitely works is a loop like so:
unsigned __int128 num = 0; for (int i = 0; i < 16; i++) { num = (num << 8) | byte[i]; } I ended up doing the follow:
union { unsigned char b[MD5_DIGEST_LENGTH]; uint64_t d[2]; unsigned __int128 q; } digest; MD5((const unsigned char *)str, length, digest.b); uint64_t tmp = digest.d[0]; digest.d[0] = be64toh(digest.d[1]); digest.d[1] = be64toh(tmp); /* digest.q is now in native byte order */
byte[i]. ;)