91视频免费?看_蜜芽MY188精品TV在线观看_国产免费无遮挡在线观看视频_深夜国产_亚洲精品欧洲精品_欧美黑人粗暴多交

  • 回復
  • 收藏
  • 點贊
  • 分享
  • 發新帖

C#轉C++(數組復制、char轉16位無符號整數)

數組復制:

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);

參數

value Byte[]

包含要轉換的兩個字節的字節數組。

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;
}

輸出:

全部回復(0)
正序查看
倒序查看
現在還沒有回復呢,說說你的想法
主站蜘蛛池模板: 迭部县| 陵川县| 镇坪县| 庄浪县| 邮箱| 新田县| 西华县| 三江| 辽宁省| 开封市| 崇州市| 吉隆县| 神木县| 南溪县| 宁德市| 海门市| 邯郸县| 重庆市| 冕宁县| 调兵山市| 双峰县| 武夷山市| 岳普湖县| 吉安市| 隆德县| 闽侯县| 宾川县| 涞源县| 江阴市| 湘潭县| 武威市| 平谷区| 明水县| 铁力市| 富锦市| 五大连池市| 石台县| 宜良县| 阿拉善盟| 江孜县| 江北区|