c_cpp Big / Little Endian解压缩整数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp Big / Little Endian解压缩整数相关的知识,希望对你有一定的参考价值。

/********************************************************************************
*
*  Copyright (c) Simon Downey <simon@ark.io> <https://github.com/sleepdefic1t>
*
*  The MIT License (MIT)
*
*  Permission is hereby granted, free of charge, to any person obtaining a copy of
*  this software and associated documentation files (the "Software"), to deal in
*  the Software without restriction, including without limitation the rights to
*  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
*  the Software, and to permit persons to whom the Software is furnished to do so,
*  subject to the following conditions:
*
*  The above copyright notice and this permission notice shall be included in all
*  copies or substantial portions of the Software.
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
*  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
*  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
*  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
*  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*********************************************************************************/
 
#include <stdint.h>

//////////////////////////////////////////////////////////////////////

uint16_t unpack2LE(uint8_t *src) {
  return ((uint16_t)src[0] & 0xFF)      |
         ((uint16_t)src[1] & 0xFF) << 8U;
}

uint32_t unpack4LE(uint8_t *src) {
  return ((uint32_t)src[0] & 0xFF)                 |
         ((uint32_t)src[1] & 0xFF)          <<  8U |
         ((uint32_t)src[2] & 0xFFFF)        << 16U |
         ((uint32_t)src[3] & 0xFFFFFFFF)    << 24U;
}

uint64_t unpack8LE(uint8_t *src) {
  return ((uint64_t)src[0] & 0xFF)                         |
         ((uint64_t)src[1] & 0xFF)                  <<  8U |
         ((uint64_t)src[2] & 0xFFFF)                << 16U |
         ((uint64_t)src[3] & 0xFFFFFFFF)            << 24U |
         ((uint64_t)src[4] & 0xFFFFFFFF)            << 32U |
         ((uint64_t)src[5] & 0xFFFFFFFFFFFFFFFF)    << 40U |
         ((uint64_t)src[6] & 0xFFFFFFFFFFFFFFFF)    << 48U |
         ((uint64_t)src[7] & 0xFFFFFFFFFFFFFFFF)    << 56U;
}

uint16_t unpack2BE(uint8_t *src) {
  return ((uint16_t)src[1] & 0xFF)  <<  8U |
         ((uint16_t)src[0] & 0xFF);
}

uint32_t unpack4BE(uint8_t *src) {
  return ((uint32_t)src[3] & 0xFFFFFFFF)    << 24U |
         ((uint32_t)src[2] & 0xFFFF)        << 16U |
         ((uint32_t)src[1] & 0xFF)          <<  8U |
         ((uint32_t)src[0] & 0xFF);
}

uint64_t unpack8BE(uint8_t *src) {
  return ((uint64_t)src[7] & 0xFFFFFFFFFFFFFFFF)    << 56U |
         ((uint64_t)src[6] & 0xFFFFFFFFFFFFFFFF)    << 48U |
         ((uint64_t)src[5] & 0xFFFFFFFFFFFFFFFF)    << 40U |
         ((uint64_t)src[4] & 0xFFFFFFFF)            << 32U |
         ((uint64_t)src[3] & 0xFFFFFFFF)            << 24U |
         ((uint64_t)src[2] & 0xFFFF)                << 16U |
         ((uint64_t)src[1] & 0xFF)                  <<  8U |
         ((uint64_t)src[0] & 0xFF);
}

//////////////////////////////////////////////////////////////////////

以上是关于c_cpp Big / Little Endian解压缩整数的主要内容,如果未能解决你的问题,请参考以下文章

有效地在 little-endian 和 big-endian 浮点数之间转换

判断处理器是Big_endian的还是Little_endian的

大端小端(Big- Endian和Little-Endian)[转]

推断CPU 是小端存储(Little endian)还是大端存储(Big endian)模式

Big Endian  和 Little Endian 模式的区别

大端和小端(big endian little endian)