溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

C#怎么使用System.Buffer以字節(jié)數(shù)組Byte[]操作基元類(lèi)型數(shù)據(jù)

發(fā)布時(shí)間:2022-05-10 14:21:55 來(lái)源:億速云 閱讀:219 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要講解了“C#怎么使用System.Buffer以字節(jié)數(shù)組Byte[]操作基元類(lèi)型數(shù)據(jù)”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“C#怎么使用System.Buffer以字節(jié)數(shù)組Byte[]操作基元類(lèi)型數(shù)據(jù)”吧!

1. Buffer.ByteLength:計(jì)算基元類(lèi)型數(shù)組累計(jì)有多少字節(jié)組成。

該方法結(jié)果等于"基元類(lèi)型字節(jié)長(zhǎng)度 * 數(shù)組長(zhǎng)度"

var bytes = new byte[] { 1, 2, 3 };
var shorts = new short[] { 1, 2, 3 };
var ints = new int[] { 1, 2, 3 };
Console.WriteLine(Buffer.ByteLength(bytes)); // 1 byte * 3 elements = 3
Console.WriteLine(Buffer.ByteLength(shorts)); // 2 byte * 3 elements = 6
Console.WriteLine(Buffer.ByteLength(ints)); // 4 byte * 3 elements = 12

2. Buffer.GetByte:獲取數(shù)組內(nèi)存中字節(jié)指定索引處的值。

public static byte GetByte(Array array, int index)

var ints = new int[] { 0x04030201, 0x0d0c0b0a };
var b = Buffer.GetByte(ints, 2); // 0x03

解析:

(1) 首先將數(shù)組按元素索引序號(hào)大小作為高低位組合成一個(gè) "大整數(shù)"。 
組合結(jié)果 : 0d0c0b0a 04030201 
(2) index 表示從低位開(kāi)始的字節(jié)序號(hào)。右邊以 0 開(kāi)始,index 2 自然是 0x03。

3. Buffer.SetByte: 設(shè)置數(shù)組內(nèi)存字節(jié)指定索引處的值。

public static void SetByte(Array array, int index, byte value)

var ints = new int[] { 0x04030201, 0x0d0c0b0a };
Buffer.SetByte(ints, 2, 0xff);

操作前 : 0d0c0b0a 04030201 
操作后 : 0d0c0b0a 04ff0201

結(jié)果 : new int[] { 0x04ff0201, 0x0d0c0b0a };

4. Buffer.BlockCopy:將指定數(shù)目的字節(jié)從起始于特定偏移量的源數(shù)組復(fù)制到起始于特定偏移量的目標(biāo)數(shù)組。

public static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)

  • src:源緩沖區(qū)。

  • srcOffset:src 的字節(jié)偏移量。

  • dst:目標(biāo)緩沖區(qū)。

  • dstOffset:dst 的字節(jié)偏移量。

  • count:要復(fù)制的字節(jié)數(shù)。

例一:arr的數(shù)組中字節(jié)0-16的值復(fù)制到字節(jié)12-28:(int占4個(gè)字節(jié)byte )

int[] arr = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
Buffer.BlockCopy(arr, 0 * 4, arr, 3 * 4, 4 * 4);
foreach (var e in arr)
{
     Console.WriteLine(e);//2,4,6,2,4,6,8,16,18,20
}

例二:

var bytes = new byte[] { 0x0a, 0x0b, 0x0c, 0x0d};
var ints = new int[] { 0x00000001, 0x00000002 };
Buffer.BlockCopy(bytes, 1, ints, 2, 2);

bytes 組合結(jié)果 : 0d 0c 0b 0a 
ints 組合結(jié)果 : 00000002 00000001 
(1) 從 src 位置 1 開(kāi)始提取 2 個(gè)字節(jié),從由往左,那么應(yīng)該是 "0c 0b"。 
(2) 寫(xiě)入 dst 位置 2,那么結(jié)果應(yīng)該是 "00000002 0c0b0001"。 
(3) ints = { 0x0c0b0001, 0x00000002 },符合程序運(yùn)行結(jié)果。

感謝各位的閱讀,以上就是“C#怎么使用System.Buffer以字節(jié)數(shù)組Byte[]操作基元類(lèi)型數(shù)據(jù)”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)C#怎么使用System.Buffer以字節(jié)數(shù)組Byte[]操作基元類(lèi)型數(shù)據(jù)這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI