c++ - How to convert endianness- as array of bits -
i'm looking convert value little-endian big-endian (and vice versa). have value expressed array of bits, rather single integer. how can implement endianness swap function?
i'm not c++ person, i'm going generically.
to convert big endian little endian reverse bytes. is, segment of 8 bits.
so if have array of n bytes (pseudocoded here):
bool bits[8*n]; you this:
for(int = 0; < n/2; i++) { for(int j = 0; j < 8; j++) { bool tmp = bits[8*i+j]; bits[8*i+j] = bits[8*(n-i-1)+j]; bits[8*(n-i-1)+j] = tmp; } } in comments, mentioned "bits" pointers expressions evaluated correct bits later. changing order of pointers in fashion yield correct change of endian-ness when later bits.
Comments
Post a Comment