c_cpp 打包/打开LE / BE

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 打包/打开LE / BE相关的知识,希望对你有一定的参考价值。

/********************************************************************************
*
*  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>

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

#define FLAG_64_BYTE        0xFFFFFFFFFFFFFFFF
#define FLAG_32_BYTE        0xFFFFFFFF
#define FLAG_16_BYTE        0xFFFF
#define FLAG_8_BYTE         0xFF

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

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

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

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

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

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

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

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

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

以上是关于c_cpp 打包/打开LE / BE的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp Arduino Midi控制器 - 更多信息可以在Boutsman.be上找到

在UTF-16中,UTF-16BE,UTF-16LE,UTF-16的端点是计算机的字节序吗?

vue打包问题:Tip: built files are meant to be served over an HTTP server.

iOS-ERROR ITMS-90717: can’t be transparent nor contain an alpha channel.

verilog 中编译错误:port must be connected to a structural net expression

使用C ++将越南语字符编码为USASCII,ISO88591,UTF8,UTF16BE,UTF16LE,UTF16