c++ - Combining two 8-Bit array to a USHORT (16 Bit) , without loop -
c++ - Combining two 8-Bit array to a USHORT (16 Bit) , without loop -
i need combine 2 uchar (8 bit) arrays ushort (16 bit) value in c. must without using "for" or loop.
as:
uchar a[1000], b[1000]; ushort c[1000]; result must as:
c[0] = {a[0], b[0]}; c[1] = {a[1], b[1]}; ... c[1000]={a[1000], b[1000]}; uint8_t 1 = 0xba; uint8_t 2 = 0xbe; uint16_t both = 1 << 8 | two;
update: maybe have not understood problem... if want convert uint8_t array uint16_t array-> check size , cast
uint8_t array[100]; uint16_t array_ptr_ushort* =(uint16_t*)&array[0]; make sure size of array 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 2 arrays in 1 contignous array without sort of loop, loop exist in underlying hardware utilize dma this...
update4:
you can 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); homecoming 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)); homecoming 0; } update5: c version 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), "arrays of different sizes"); int _tmain(int argc, _tchar* argv[]) { int size = sizeof(array1); int count = 0; { array_combined[count] = (array2[count] << 8) | array1[count]; }while(count++ != size); homecoming 0; } update6: there c++ ways accomplish this...
c++ c
Comments
Post a Comment