组合两个8位数组到USHORT(16位),无循环 [英] Combining two 8-Bit array to a USHORT (16 Bit) , without loop

查看:273
本文介绍了组合两个8位数组到USHORT(16位),无循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将两个 UCHAR (8位)数组合并到C中的 USHORT



as:

  UCHAR A [1000],B [1000]; 
USHORT C [1000];

结果必须为:

  C [0] = {A [0],B [0]}; 
C [1] = {A [1],B [1]};
...
C [1000] = {A [1000],B [1000]};


解决方案

  uint8_t one = 0xBA; 
uint8_t two = 0xBE;

uint16_t both = one<< 8 |二;



UPDATE:
也许我还没有理解你的问题。 ..但是如果你想将一个uint8_t数组转换为一个uint16_t数组 - >检查大小和转换

  uint8_t array [100] ; 
uint16_t array_ptr_ushort * =(uint16_t *)& array [0];

确保数组的大小是偶数。



UPDATE2:

  uint8_t array1 [100] 
uint8_t array2 [100];
uint16_t combined [100];

memcpy(array1,sizeof(array1))
memcpy((uint8_t *)combined + sizeof(array1),array2,sizeof(array2))

UPDATE3:



两个数组在一个连续数组中没有某种循环,该循环将存在于底层硬件中,即使您使用DMA为此...



UPDATE4: / strong>



可以递归执行。

  #include stdafx.h
#include< cstdint>
#include< algorithm>

uint8_t arrayA [] = {1,2,3,4,5,6,7,8,9,10};
uint8_t arrayB [] = {0xA,0x9,0x8,0x7,0x6,0x5,0x4,0x3,0x2,0x1};
uint16_t array_combined [sizeof(arrayA)] = {};
static_assert(sizeof(arrayA)== sizeof(arrayB),不同大小的数组);

uint16_t combine(const uint8_t * a,const uint8_t * b,uint16_t * put,uint32_t size)
{
uint16_t value =(* a << 8) * b;
if(size)
* put = combine(++ a,++ b,++ put,--size);
返回值;
}

void combine_arrays(const uint8_t * a,const uint8_t * b,uint16_t * put,uint32_t size)
{
* put = combine ,put,size);
}

int _tmain(int argc,_TCHAR * argv [])
{

combine_arrays(arrayA,arrayB,array_combined,sizeof );

return 0;
}

UPDATE5: C版本搭配static_assert从C ++

  #include< stdint.h> 


uint8_t array1 [] = {1,2,3,4,5,6,7,8,9,10};
uint8_t array2 [] = {0xA,0x9,0x8,0x7,0x6,0x5,0x4,0x3,0x2,0x1};
uint16_t array_combined [sizeof(array1)] = {};
static_assert(sizeof(array1)== sizeof(array2),不同大小的数组);

int _tmain(int argc,_TCHAR * argv [])
{

int size = sizeof(array1);
int count = 0;
do
{
array_combined [count] =(array2 [count]<< 8)| array1 [count];
} while(count ++!= size);

return 0;
}

UPDATE6: this ...


I need to combine two UCHAR (8 Bit) arrays to a USHORT (16 Bit) value in C. But I must do that without using "for" or any loop.

as:

UCHAR A[1000], B[1000];
USHORT C[1000];

result must be as:

C[0] = {A[0], B[0]};
C[1] = {A[1], B[1]};
...
C[1000]={A[1000], B[1000]};

解决方案

uint8_t one = 0xBA;
uint8_t two = 0xBE;

uint16_t both = one << 8 | two;

UPDATE: Maybe I have not understood your problem... but if you want to convert a uint8_t array to a uint16_t array-> check size and cast

uint8_t array[100];
uint16_t array_ptr_ushort* =(uint16_t*)&array[0];

Make sure the size of the array is even.

UPDATE2:

uint8_t array1[100];
uint8_t array2[100];
uint16_t combined[100];

memcpy(combined, array1, sizeof(array1))
memcpy((uint8_t*)combined + sizeof(array1), array2, sizeof(array2))

UPDATE3:

You can not combine two arrays in one contignous array without some sort of loop, the loop will exist in the underlying hardware even you use DMA for this...

UPDATE4:

You can do it recursively.

#include "stdafx.h"
#include <cstdint>
#include <algorithm>

uint8_t arrayA[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
uint8_t arrayB[] = {0xA, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1};
uint16_t array_combined[sizeof(arrayA)] = {};
static_assert(sizeof(arrayA) == sizeof(arrayB), "Arrays of different sizes");

uint16_t combine(const uint8_t *a, const uint8_t *b, uint16_t *put, uint32_t size)
{
    uint16_t value = (*a << 8) | *b;
    if(size)
        *put = combine(++a, ++b, ++put, --size);
    return value;
}

void combine_arrays(const uint8_t *a, const uint8_t *b, uint16_t *put, uint32_t size)
{
    *put = combine(a, b, put, size);
}

int _tmain(int argc, _TCHAR* argv[])
{

    combine_arrays(arrayA, arrayB, array_combined, sizeof(arrayA));

return 0;
}

UPDATE5: C version with static_assert from C++

#include <stdint.h>


uint8_t array1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
uint8_t array2[] = {0xA, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1};
uint16_t array_combined[sizeof(array1)] = {};
static_assert(sizeof(array1) == sizeof(array2), "Arrays of different sizes");

int _tmain(int argc, _TCHAR* argv[])
{

    int size = sizeof(array1);
    int count = 0;
    do
    {
        array_combined[count] = (array2[count] << 8) | array1[count];
    }while(count++ != size);

    return 0;
}

UPDATE6: There are also C++ ways to achieve this...

这篇关于组合两个8位数组到USHORT(16位),无循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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