how to manipulate char array [closed]

拟墨画扇 提交于 2020-02-07 08:24:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!