數(shù)組復(fù)制:
C#實(shí)現(xiàn):
Copy(Array, Int32, Array, Int32, Int32)
復(fù)制 Array 中的一系列元素(從指定的源索引開始),并將它們粘貼到另一 Array 中(從指定的目標(biāo)索引開始)。 長度和索引指定為 32 位整數(shù)。
c++實(shí)現(xiàn):
1.字符數(shù)組
使用strcpy
2.int,float,double等數(shù)組
使用memcpy,如復(fù)制一個(gè)長度為5 的 float數(shù)組,則代碼示例如下
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));
注意,第三個(gè)參數(shù)不是指數(shù)組個(gè)數(shù),而是指要復(fù)制的數(shù)據(jù)的總字節(jié)數(shù)長度
字節(jié)數(shù)組(C++中unsigned char)轉(zhuǎn)16位整數(shù)
C#實(shí)現(xiàn):
[System.CLSCompliant(false)]
public static ushort ToUInt16 (byte[] value, int startIndex);
參數(shù)
value Byte[]
包含要轉(zhuǎn)換的兩個(gè)字節(jié)的字節(jié)數(shù)組。
startIndex Int32
value
內(nèi)的起始位置。
返回UInt16
由兩個(gè)字節(jié)構(gòu)成、從 startIndex
開始的 16 位無符號(hào)整數(shù)。
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++中無符號(hào)字節(jié)數(shù)組轉(zhuǎn)無符號(hào)16位整數(shù)實(shí)現(xiàn):
#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));//十進(jìn)制
printf("uint8_t:%x %x\n", *(uint8_t*)ch, *(uint8_t*)(ch + 1));//十六進(jìn)制
printf("uint16_t:%d %d\n", *(uint16_t*)ch, *(uint16_t*)(ch + 1));//十進(jìn)制
printf("uint16_t:%d %d\n", *(uint16_t*)(ch+2), *(uint16_t*)(ch + 3));//十進(jìn)制
printf("uint16_t:%x %x\n", *(uint16_t*)ch, *(uint16_t*)(ch + 1));//十六進(jìn)制
system("pause");
return 0;
}
輸出: