添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
[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] 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
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)
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.
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •