大端模式和小端模式相互转换,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) { } }

 

你可能感兴趣的:(C/C++)