c语言大端小端的程序,大端模式和小端模式相互转换,C语言方式实现

大端模式和小端模式相互转换,C语言方式实现

GitHub仓库:https://github.com/XinLiGH/BigAndLittleEndianConversion

PS:博文不再更新,后续更新会在GitHub仓库进行。

大端模式和小端模式相互转换,C语言方式实现。程序中涉及到数据存放顺序的概念,详细介绍见维基百科[Endianness](https://en.wikipedia.org/wiki/Endianness)。

1,开发环境

1,操作系统:Windows 10 专业版

2,IDE:Visual Studio 2017 专业版

2,程序源码

main.c文件

/**

******************************************************************************

* @file main.c

* @author XinLi

* @version v1.0

* @date 24-May-2020

* @brief Main program body.

******************************************************************************

* @attention

*

*

Copyright © 2020 XinLi

*

* This program is free software: you can redistribute it and/or modify

* it under the terms of the GNU General Public License as published by

* the Free Software Foundation, either version 3 of the License, or

* (at your option) any later version.

*

* This program is distributed in the hope that it will be useful,

* but WITHOUT ANY WARRANTY; without even the implied warranty of

* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

* GNU General Public License for more details.

*

* You should have received a copy of the GNU General Public License

* along with this program. If not, see .

*

******************************************************************************

*/

/* Header includes -----------------------------------------------------------*/

#include #include /* Macro definitions ---------------------------------------------------------*/

/* Type definitions ----------------------------------------------------------*/

/* Variable declarations -----------------------------------------------------*/

/* Variable definitions ------------------------------------------------------*/

/* Function declarations -----------------------------------------------------*/

static void BigAndLittleEndianConversion(void *data, uint32_t size);

/* Function definitions ------------------------------------------------------*/

/**

* @brief Big and little endian conversion.

* @param [in] data: The pointer of the data to be converted.

* @param [in] size: The size of the data to be converted.

* @return None.

*/

static void BigAndLittleEndianConversion(void *data, uint32_t size)

{

for(uint32_t i = 1; i < size; i++)

{

for(uint32_t j = 1; j < size - i + 1; j++)

{

uint8_t c = *(((uint8_t *)data) + j - 1);

*(((uint8_t *)data) + j - 1) = *(((uint8_t *)data) + j);

*(((uint8_t *)data) + j) = c;

}

}

}

/**

* @brief Main program.

* @param None.

* @return None.

*/

int main(void)

{

uint8_t littleEndianDataBuffer[4] = {(uint8_t)(123456789>>0), (uint8_t)(123456789>>8), (uint8_t)(123456789>>16), (uint8_t)(123456789>>24)};

uint8_t bigEndianDataBuffer[4] = {(uint8_t)(123456789>>24), (uint8_t)(123456789>>16), (uint8_t)(123456789>>8), (uint8_t)(123456789>>0)};

uint32_t littleEndianData = *(uint32_t *)littleEndianDataBuffer;

uint32_t bigEndianData = *(uint32_t *)bigEndianDataBuffer;

BigAndLittleEndianConversion(&bigEndianData, sizeof(bigEndianData));

if(littleEndianData == bigEndianData)

{

printf("bigEndianData = %u", bigEndianData);

}

while(1)

{

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值