问题
I have something like this.
char header[4096]; //header information from a file
there is a field called startByteOffset
which is 8 bytes
(offset in header is 8 too) and then there is endByteOffset
which is 8 bytes
in lenght too.(offset in header is 16).
I need to change the endByteOffset
. How to do that? Thanks.
Let me know if it is not clear enough.
回答1:
To break an integer into characters (bytes) you can use shifting and masking.
uint64_t value = /* ... */;
header[endbyteoffset] = value & 0xff;
header[endbyteoffset+1] = (value >> 8) & 0xff;
header[endbyteoffset+2] = (value >> 16) & 0xff;
header[endbyteoffset+3] = (value >> 24) & 0xff;
// ...
header[endbyteoffset+7] = (value >> 56) & 0xff;
You'll need to know if the values are to be stored in little-endian or big-endian order. The example I provided above is for little-endian; for big-endian you just reverse the order of the shifts, starting with the largest shift first.
来源:https://stackoverflow.com/questions/12391672/how-to-manipulate-char-array