如何重新排序整数的字节? [英] How can I reorder the bytes of an integer?

查看:86
本文介绍了如何重新排序整数的字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是将数据文件从大端转换为小端&反之亦然。使用C。相反,我已经在网上寻找了大约3个小时的其他示例并阅读了我的教科书,但是,我对如何启动此功能一无所知。

My task is to convert a data file from big endian to little endian & vice versa using C. I have been looking online for about 3 hours now for other examples and reading my text book, however I am so stuck on how to even start this function.

到目前为止,我的事件顺序是正确的(1到4),但是在我的 convert_and_save 函数内部,我必须使用→创建一个char数组。 char buffer [4];

So far I have the order of events correct (1 through 4) but inside my convert_and_save function do I have to create a char array using → char buffer[4];?

有人可以帮助我吗?即使您只是提供有关查找内容的线索,我也将不胜感激。

Can someone please help me? even if you just give me clues on what to look up, I would greatly appreciate it.

我需要编写一个函数:

void convert_and_save(struct record item, FILE * output_handle, int number);

在此功能内,我执行以下一系列步骤:

inside this function I do the following series of steps:


  1. 使用以下方法将整数转换为字符数组:

  1. Convert the integer into an array of chars using:

int integer_to_characters(int number, char * buffer)
{
   memcpy(buffer, &number, 4);
}


  • 反转该数组中字符的顺序。

  • Reverse the order of the chars in that array.

    使用以下方式将字符数组转换回整数:

    Convert the array of chars back to an integer using:

    int characters_to_integer(char * buffer)
    {
       int result;
       memcpy(&result, buffer, 4);
       return result;
    }
    


  • 使用以下命令将转换后的记录写入输出文件:

  • write the converted record on the output file using:

    void save_record(FILE * file_handle, struct record a)
    {
       char output_buffer[size_of_record];
       integer_to_characters(a.age, &(output_buffer[0]));
       memcpy(&(output_buffer[4]), a.name, 12);
       integer_to_characters(a.department, &(output_buffer[16]));
       fwrite(output_buffer, sizeof(char), size_of_record, file_handle);
    }
    



  • 推荐答案

    这些不是标准功能(这些是gcc扩展名),但是您可以使用它们:

    These are not standard function (these are gcc extensions), but you may be able to use them:


    内置函数:uint16_t __builtin_bswap16(uint16_t x)返回x
    ,其字节顺序相反;例如,0xaabb变为
    0xbbaa。字节在这里始终意味着恰好是8位。

    — Built-in Function: uint16_t __builtin_bswap16 (uint16_t x) Returns x with the order of the bytes reversed; for example, 0xaabb becomes 0xbbaa. Byte here always means exactly 8 bits.

    —内置函数:uint32_t __builtin_bswap32(uint32_t x)与__builtin_bswap16类似的
    ,除了参数和返回类型是

    — Built-in Function: uint32_t __builtin_bswap32 (uint32_t x) Similar to __builtin_bswap16, except the argument and return types are 32 bit.

    —内置函数:uint64_t __builtin_bswap64(uint64_t x)与__builtin_bswap32类似的
    ,只是参数和返回类型为64位。 p>

    — Built-in Function: uint64_t __builtin_bswap64 (uint64_t x) Similar to __builtin_bswap32, except the argument and return types are 64 bit.

    如果可以使用它们,很可能会为您的平台带来更优化的代码,否则,请提交补丁到gcc:)

    If you can use these, chances are it will result in more optimal code for your platform, if not, good job, submit a patch to gcc :)

    Clang也有 __ builtin_bswap16()__builtin_bswap32()__builtin_bswap64()

    Visual Studio

    Visual Studio

    unsigned short _byteswap_ushort (
       unsigned short val
    );
    unsigned long _byteswap_ulong (
       unsigned long val
    );
    unsigned __int64 _byteswap_uint64 (
       unsigned __int64 val
    );
    

    ICC具有 _bswap16,_bswap和_bswap64 *需要更多参考

    ICC has _bswap16, _bswap and _bswap64 *need further reference

    这篇关于如何重新排序整数的字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆