Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use System.Buffer to manipulate primitive type data with byte array Byte []

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "C # how to use System.Buffer to operate primitive type data with byte array Byte []". The content of the article is simple and clear, and it is easy to learn and understand. Please follow Xiaobian's train of thought to study and learn "C# how to use System.Buffer to operate primitive type data with byte array Byte []".

1. Buffer.ByteLength: calculates the cumulative number of bytes in an array of primitive types.

The result of this method is equal to "primitive type byte length * array length".

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 = 3Console.WriteLine (Buffer.ByteLength (shorts)); / / 2 byte * 3 elements = 6Console.WriteLine (Buffer.ByteLength (ints)); / / 4 byte * 3 elements = 122. Buffer.GetByte: gets the value at the specified index of bytes in the array memory.

Public static byte GetByte (Array array, int index)

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

Parsing:

(1) first, the array is combined into a "big integer" according to the size of the element index number as high and low bits.

Combination result: 0d0c0b0a 04030201

(2) index represents the byte sequence number starting from the low bit. Starting with 0 on the right, index 2 is naturally 0x03.

3. Buffer.SetByte: sets the value at the specified index of the array memory byte.

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

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

Before operation: 0d0c0b0a 04030201

After operation: 0d0c0b0a 04ff0201

Result: new int [] {0x04ff0201, 0x0d0c0b0a}

4. Buffer.BlockCopy: copies a specified number of bytes from a source array starting at a specific offset to a target array starting at a specific offset.

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

Src: source buffer.

Byte offset of the srcOffset:src.

Dst: target buffer.

Byte offset of the dstOffset:dst.

Count: the number of bytes to copy.

Example 1: the value of byte 0-16 in the array of arr is copied to byte 12-28: (int occupies 4 bytes byte)

Int [] arr = {2 Console.WriteLine 4 arr = 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, 4, 6, 4, 4, 6, 16, 18, 20}

Example 2:

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

Bytes combination result: 0d 0c 0b 0a

Ints combination result: 00000002 00000001

(1) extract 2 bytes from src position 1, from left to left, then it should be "0c 0b".

(2) write dst position 2, then the result should be "00000002 0c0b0001".

(3) ints = {0x0c0b0001, 0x00000002}, which accords with the running result of the program.

Thank you for your reading, the above is the content of "how to use System.Buffer to operate primitive type data with byte array Byte []". After the study of this article, I believe you have a deeper understanding of how to use System.Buffer to operate primitive type data with byte array Byte []. The specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report