Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
to convert array of uint16 having 5 elements to array of uint8

[edit]
From OP in comment to one of the solutions (explains more thoroughly):
"thanx for replying but i want to separate both bytes of uint16 and then the total 10 bytes which , i will get should be in array of uint8"
[/edit]
Posted
Updated 8-Jun-11 7:31am
v2
Comments
ThatsAlok 8-Jun-11 6:27am    
it's not difficult, but there could be loss of data!
Sergey Alexandrovich Kryukov 8-Jun-11 13:21pm    
OP did not explain it yet, bit I guess it should break 16-bit words into bytes with no loss. uint16 arr1[2] -> uint8 arr2[4]...
--SA

This discussion[^] may help you.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Jun-11 13:25pm    
There are answers inside, a 5.
--SA
Abhinav S 9-Jun-11 2:20am    
Thank you.
Albert Holguin 8-Jun-11 13:38pm    
the description of the union within would be perfect for this application as well, wouldn't require any looping whatsoever, my 5
Abhinav S 9-Jun-11 2:20am    
Thank you.
Another method is playing with the pointers...
uint16 arr1[5] = {2, 3, 20, 90, 3857};
uint8  arr2[10] = {0};

uint8* ptr = (uint8 *) &arr1; //cast the 16bit pointer to an 8bit pointer
for(int i=0; i<10; i++)
{
 arr2[i] = *ptr; //pass data to other array
 ptr++;          //move your pointer
}
 
Share this answer
 
Comments
Espen Harlinn 8-Jun-11 11:51am    
Good reply Albert, my 5
Albert Holguin 8-Jun-11 12:52pm    
thank you espen
Sergey Alexandrovich Kryukov 8-Jun-11 13:18pm    
Sure, simple effective way to break into bytes, a 5.
--SA
Albert Holguin 8-Jun-11 13:28pm    
Yeah, I've used this before for data packing/unpacking... thanks SA
Sergey Alexandrovich Kryukov 8-Jun-11 18:22pm    
I saw also a union-based approach, but it depends on where you need to have independent read/write "clones". Your solution already makes it separate independent objects.
--SA
Just use Alberts third line of code:
for(int i = 0; i < 10; i++)
{
  cout << ptr[i] << endl;
}


Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Jun-11 13:25pm    
What question do you ask by this post?
--SA
Espen Harlinn 8-Jun-11 14:39pm    
In his reply to ThatsAlok d_singh wrote: thanx for replying but i want to separate both bytes of uint16 and then the total 10 bytes which , i will get should be in array of uint8.

My guess is that he wants a sequence of bytes, and doesn't actually need to copy the data ...
Sergey Alexandrovich Kryukov 8-Jun-11 18:21pm    
I see. Thinks depend on that. For example, I saw a solution based on union: it would need extra step (or it's not good enough) if the arrays should be independent objects (clones) and not read-only. 5 for the note.
--SA
Espen Harlinn 8-Jun-11 19:46pm    
Personally I prefer casting in most cases ( less work :blush: ), but a union may allow the intent of the code to become clearer. I'm well aware that littering the code with casts this way becomes a real pain if the client suddenly wants you to support communication with a system using a different byteordering :) - one workaround is to include information about senders byte order in the packet header - similar to OMG.org IIOP (corba)
Sergey Alexandrovich Kryukov 8-Jun-11 23:32pm    
Sure, agree.
--SA
For the sake of completeness on the subject, another method:
union MB_DATA
{
  uint16 b16[5];
  uint8  b8[10];
};

MB_DATA data = {2,3,20,90,3857}; //this will already contain both 16 and 8 bit representations


Should be noted that with this solution, the two methods of accessing the data are dependent, meaning you change something in one and it changes on both.... unions don't allocate memory independently for members.
 
Share this answer
 
v2
Comments
Espen Harlinn 8-Jun-11 19:37pm    
Another 5 :)
Albert Holguin 8-Jun-11 19:42pm    
thanks espen... :)
isn't this simple

uint16 arr[5]= some values;
uint8 arr1[5]

for(i=0 to 5)
arr1[i]=arr[i]
 
Share this answer
 
Comments
d_singh 8-Jun-11 7:08am    
thanx for replying but i want to separate both bytes of uint16 and then the total 10 bytes which , i will get should be in array of uint8
Richard MacCutchan 8-Jun-11 7:13am    

// add another index
arr1[i] = arr[j] >> 8;
arr1[i+1] = arr[j] & 0xFF;

This is basic data manipulation using standard operators that you really should know if you are already writing code.
d_singh 8-Jun-11 8:17am    
thanx
Albert Holguin 8-Jun-11 13:30pm    
this probably shouldn't have been downvoted since OP hadn't quite explained himself completely yet.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900