數組復制:
C#實現:
Copy(Array, Int32, Array, Int32, Int32)
復制 Array 中的一系列元素(從指定的源索引開始),并將它們粘貼到另一 Array 中(從指定的目標索引開始)。 長度和索引指定為 32 位整數。
。
c++實現:
1.字符數組
使用strcpy
2.int,float,double等數組
使用memcpy,如復制一個長度為5 的 float數組,則代碼示例如下
int len = 5;
float a[len] = {1.0 ,1.1, 1.2, 1.3, 1.4};
float b[len];
memset(b, a, len*sizeof(float));
memcpy(b, a, len * sizeof(float));
注意,第三個參數不是指數組個數,而是指要復制的數據的總字節數長度
字節數組(C++中unsigned char)轉16位整數
C#實現:
[System.CLSCompliant(false)]
public static ushort ToUInt16 (byte[] value, int startIndex);
參數
startIndex Int32
value
內的起始位置。
返回UInt16
由兩個字節構成、從 startIndex
開始的 16 位無符號整數。
byte[] byteArray = {
15, 0, 0, 255, 3, 16, 39, 255, 255, 127 };
BitConverter.ToUInt16( byteArray, 1 );
BitConverter.ToUInt16( byteArray, 0 );
BitConverter.ToUInt16( byteArray, 3 );
BitConverter.ToUInt16( byteArray, 5 );
BitConverter.ToUInt16( byteArray, 8 );
BitConverter.ToUInt16( byteArray, 7 );
輸出:
index array elements ushort
----- -------------- ------
1 00-00 0
0 0F-00 15
3 FF-03 1023
5 10-27 10000
8 FF-7F 32767
7 FF-FF 65535
C++中無符號字節數組轉無符號16位整數實現:
#include <iostream>
using namespace std;
int main()
{
unsigned char ch[4] = { 0xAA,0x11,0x02,0x04 }; //-----》使用uchar
printf("%d %d\n", *ch, *(ch + 1));
printf("%x %x\n", *ch, *(ch + 1));
printf("uint8_t:%d %d\n", *(uint8_t*)ch, *(uint8_t*)(ch + 1));//十進制
printf("uint8_t:%x %x\n", *(uint8_t*)ch, *(uint8_t*)(ch + 1));//十六進制
printf("uint16_t:%d %d\n", *(uint16_t*)ch, *(uint16_t*)(ch + 1));//十進制
printf("uint16_t:%d %d\n", *(uint16_t*)(ch+2), *(uint16_t*)(ch + 3));//十進制
printf("uint16_t:%x %x\n", *(uint16_t*)ch, *(uint16_t*)(ch + 1));//十六進制
system("pause");
return 0;
}
輸出: