C#使用指针复制字节数组
下面的示例使用指针将字节从一个数组复制到另一个数组。
此示例使用 unsafe 关键字,它使您能够在 Copy 方法中使用指针。 fixed 语句用于声明指向源数组和目标数组的指针。 这将锁定源数组和目标数组在内存中的位置,使其不会因为垃圾回收操作而移动。
这些数组的内存块将在 fixed 块结束时取消锁定。 因为此示例中的 Copy 方法使用了 unsafe 关键字,所以必须使用 /unsafe 编译器选项编译此方法。
若要在 Visual Studio 中设置选项,请右击项目名称,然后单击“属性”。 在“生成”选项卡上,选择“允许不安全代码”。
class TestCopy
{
// The unsafe keyword allows pointers to be used in the following method. static unsafe void Copy(byte[] source, int sourceOffset, byte[] target,
int targetOffset, int count)
{
// If either array is not instantiated, you cannot complete the copy.
if ((source == null) || (target == null))
{
throw new System.ArgumentException();
} // If either offset, or the number of bytes to copy, is negative, you
// cannot complete the copy.
if ((sourceOffset < ) || (targetOffset < ) || (count < ))
{
throw new System.ArgumentException();
} // If the number of bytes from the offset to the end of the array is
// less than the number of bytes you want to copy, you cannot complete
// the copy.
if ((source.Length - sourceOffset < count) ||
(target.Length - targetOffset < count))
{
throw new System.ArgumentException();
} // The following fixed statement pins the location of the source and
// target objects in memory so that they will not be moved by garbage
// collection.
fixed (byte* pSource = source, pTarget = target)
{
// Set the starting points in source and target for the copying.
byte* ps = pSource + sourceOffset;
byte* pt = pTarget + targetOffset; // Copy the specified number of bytes from source to target.
for (int i = ; i < count; i++)
{
*pt = *ps;
pt++;
ps++;
}
}
} static void Main()
{
// Create two arrays of the same length.
int length = ;
byte[] byteArray1 = new byte[length];
byte[] byteArray2 = new byte[length]; // Fill byteArray1 with 0 - 99.
for (int i = ; i < length; ++i)
{
byteArray1[i] = (byte)i;
} // Display the first 10 elements in byteArray1.
System.Console.WriteLine("The first 10 elements of the original are:");
for (int i = ; i < ; ++i)
{
System.Console.Write(byteArray1[i] + " ");
}
System.Console.WriteLine("\n"); // Copy the contents of byteArray1 to byteArray2.
Copy(byteArray1, , byteArray2, , length); // Display the first 10 elements in the copy, byteArray2.
System.Console.WriteLine("The first 10 elements of the copy are:");
for (int i = ; i < ; ++i)
{
System.Console.Write(byteArray2[i] + " ");
}
System.Console.WriteLine("\n"); // Copy the contents of the last 10 elements of byteArray1 to the
// beginning of byteArray2.
// The offset specifies where the copying begins in the source array.
int offset = length - ;
Copy(byteArray1, offset, byteArray2, , length - offset); // Display the first 10 elements in the copy, byteArray2.
System.Console.WriteLine("The first 10 elements of the copy are:");
for (int i = ; i < ; ++i)
{
System.Console.Write(byteArray2[i] + " ");
}
System.Console.WriteLine("\n");
}
}
/* Output:
The first 10 elements of the original are:
0 1 2 3 4 5 6 7 8 9 The first 10 elements of the copy are:
0 1 2 3 4 5 6 7 8 9 The first 10 elements of the copy are:
90 91 92 93 94 95 96 97 98 99
*/
C#使用指针复制字节数组的更多相关文章
- 比较C#中几种常见的复制字节数组方法的效率
在日常编程过程中,我们可能经常需要Copy各种数组,一般来说有以下几种常见的方法:Array.Copy,IList<T>.Copy,BinaryReader.ReadBytes,Buffe ...
- 比较C#中几种常见的复制字节数组方法的效率[转]
[原文链接] 在日常编程过程中,我们可能经常需要Copy各种数组,一般来说有以下几种常见的方法:Array.Copy,IList<T>.Copy,BinaryReader.ReadByte ...
- C#指针与字节数组的操作
private static byte[] ReadBytesFromPtr(IntPtr intPtr, int bufferLength) { var result = new byte[buff ...
- C#使用内存和指针方式将字节数组转换为Bitmap
/// <summary> /// 指针方式转 /// </summary> /// <param name="Width">图像的宽</ ...
- C和C指针小记(十三)-数组
1.1 一维数组 一维数组的声明: int a[10]; 这里a就是一个数组. 数组a的类型就是一个指向整型的常量指针. 但是数组和指针是**不相同**的. **数组具有特定数量的元素,而指针只是一个 ...
- 字节数组与String类型的转换
还是本着上篇文章的原则,只不过在Delphi中string有点特殊! 先了解一下Delphi中的string 1. string = AnsiString = 长字符串,理论上长度不受限制,但其实受限 ...
- C和指针 第八章 数组
8.1 数组名和指针 int a; int b[10]; a称为一个标量,表示一个单一的值,变量的类型是整数. b是数组,b[1]的类型是整数,b是一个指针常量,表示数组第一个元素的地址.b的类型取决 ...
- (IEEE-754) 字节数组与浮点数之间的互相转换(MODBUS float类型)
在做上位机开发过程中,经常会碰到字节数组与浮点数,整数等数据之间的转换,有时为了验证数据是否解析正确,得借助于IEEE浮点数工具,本文把基于c#实现的浮点数与字节数组(或16进制的字符串)转换的实现方 ...
- Java:IO流其他类(字节数组流、字符数组流、数据流、打印流、Properities、对象流、管道流、随机访问、序列流、字符串读写流)
一.字节数组流: 类 ByteArrayInputStream:在构造函数的时候,需要接受数据源,而且数据源是一个字节数组. 包含一个内部缓冲区,该缓冲区包含从流中读取的字节.内部计数器跟踪 read ...
随机推荐
- jdbc 配置properties实现
package com.web.study; import java.io.InputStream; import java.sql.Connection; import java.sql.Drive ...
- Java 线程综述
线程重在 线程同步和线程通信的编程 1.线程与进程? 线程是指程序在执行过程中,能够执行程序代码的一个执行单元.线程的状态:运行.就绪.挂起(suspend).结束; 进程是指一段正在执行的程序. ...
- SQL Server数据库(表的创建)
表的创建 1.创建列(字段):列名+类型 2.设置主键列:能够唯一表示一条数据 3.设置唯一键:设计--索引/键--添加--唯一键(选择列)--确定 唯一键的内容不能重复 4.外键关系:一张表(从表) ...
- mouseover和mouseout多次触发解决方法(兼容ie和firefox)(转)
在用到mouseover和mouseout事件来作为事件触发的条件,但是如果我们用做触发的元素内部有其他的元素的时候当鼠标移上的时候会反复的触发mouseover和mouseout事件,如导致菜单闪烁 ...
- Eclipse 反编译器
Help-->Install New SoftWare 贴上反编译地址:http://opensource.cpupk.com/decompiler/update/ 选择add,一路向北,起飞.
- 利用VBoxManage对虚拟机格式vdi、vmdk、vhd进行互转
虚拟机顾名思义就是虚拟出来的机器(virtual machine),虚拟化技术也是时下IT界最热门的技术,因其能更加有效利用硬件资源,整合IT应用,降低TCO,节能环保等,说白了就是一台硬件上够强 ...
- Gramar
一.And 并列关系(and) in addition / and / similarly / likewise / as well as / besides / furthermore / also ...
- VBA读取固定文件夹中txt内容
Sub OneTxt() '打开一个txt文件 Dim Filename As Variant, extLine&, mArr() As String Dim i%, j%, txtpath ...
- Hibernate的generator属性之意义
Hibernate的generator属性之意义 本文讲述Hibernate的generator属性的意义.Generator属性有7种class,本文简略描述了这7种class的意义和用法. Hib ...
- 使用HTTP访问网络------使用HTTPURLConnection
HTTPURLConnection继承了URLConnection,因此也可用于向指定网站发送GET请求.POST请求.它在URLConnection的基础上提供了如下便捷的方法: 1.int ge ...