将字节数组从big endian转换为little endian?

时间:2018-02-14 14:01:04

标签: c arrays pointers

我的程序在一个大数据库的长数组中转储一堆字节,我的目标是将字节分成双字(4字节组)并将它们放在小端格式中。这是我到目前为止获得的代码:

int print_got_bytes(struct _section *got_section, Elf_Data *data) {
    size_t size = data->d_size;
    off_t offset = data->d_off;
    unsigned char *buf = (unsigned char *) data->d_buf;

    unsigned char *data_bytes;
    printf("%ld\n", size);

    //for(int cnt = 0; cnt < size; cnt += 4) {
        for(int i = 3; i >= 0; i--) {
            data_bytes[3 - i] = buf[i];
            printf("%02x", data_bytes[3 - i]);
        }
    //}
    puts("");
    return 0;
}

到目前为止,这适用于第一组字节,但我无法弄清楚如何递增指针data_bytes并重复下一组字节的过程。这是解决这个问题的错误方法吗?

0 个答案:

没有答案