How to concatenate elements in a char Array and store in a short Array

Hello,
I am working on this project where I am receiveing a char array and need to concatenate elements and store the combined element in a short array.
For Example:
I have a char array with the following elements:FF,78,98,45,67,12,23,45,65,78

and I want the short array to have the following elements:FF78,9845,6712,2345,6578.

I have tried strcat() but it didn't work.

Any help/advice will be greatly appreciated.

Thanks In Advance
The result depends on whether your system uses big endian or little endian.

But in any case you can try at first a simple copying

1
2
3
4
5
6
7
8
9
10
11
12
char a[] = { 0xFF, 78, 98, 45, 67, 12, 23, 45, 65, 78 };
short b[( sizeof( a ) + 1 ) / 2] = {};

std::copy( std::begin( a ), std::end( a ), reinterpret_cast<char *>( b ) );


for ( auto x = b; x < b + ( sizeof( a ) + 1 ) / 2; ++x )
{
	std::cout << std::hex << *x << ' ';
}

std::cout << std::endl;


I forgot to mention that it can be correct provided that sizeof( short ) == 2.:)
Last edited on
Topic archived. No new replies allowed.