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 ...
随机推荐
- jQuery+zTree加载树形结构菜单
jQuery+zTree加载树形结构菜单 由于项目中需要设计树形菜单功能,经过一番捣腾之后,终于给弄出来了,所以便记下来,也算是学习zTree的一个总结吧. zTree的介绍: 1.zTree 是利用 ...
- for循环语句以及迭代法和穷举法
循环语句: 四要素:初始条件,循环条件,状态改变,循环体 for(初始条件;循环条件;状态改变){ //循环体} 案例1:打印等腰直角三角形和菱形 左上三角 static void Main(stri ...
- Java多线程-新特性-线程池
Sun在Java5中,对Java线程的类库做了大量的扩展,其中线程池就是Java5的新特征之一,除了线程池之外,还有很多多线程相关的内容,为多线程的编程带来了极大便利.为了编写高效稳定可靠的多线程程序 ...
- jquery删除原事件
$(document).ready(function(){ var fn = $("#exportCsv").attr( "onclick" ); // 获取原 ...
- PowerMock.expectNew(Class<T> type, Class<?>[] parameterTypes, Object... arguments)
1:PowerMock.expectNew(Class<T> type, Class<?>[] parameterTypes, Object... arguments) 如果你 ...
- 使用WBI SAP Adapter 实现IDoc的同步处理(转)
1. 应用背景 某汽车制造企业(以下称为厂商A)与其仓储系统提供商(以下称为厂商B)需要进行数据交换.汽车厂商A使用SAP系统作ERP管理,所有数据都要进入SAP进行处理,仓储系统提供商使用的是自有的 ...
- Diskpart使用说明
[查看硬盘信息] 1.打开命令窗口 cmd 2.diskpart 命令进入Diskpart管理程式 3.list disk 查看硬盘信息 list partition 查看分区信息 [初使化硬盘] ...
- 用K2 smartforms开发一个应用程序究竟比ASP.NET快多少?
这次试验的起因是一场内部辩论. “用K2 smartforms开发一个应用程序究竟比ASP.NET快多少?” 我们推测是快4倍. 但是经过测试发现,我们推测错了. 本文记录了试验的规划.过程以及令人惊 ...
- 傅里叶变换:MP3、JPEG和Siri背后的数学
九年前,当我还坐在学校的物理数学课的课堂里时,我的老师为我们讲授了一种新方法,给我留下了深刻映像.我认为,毫不夸张地说,这是对数学理论发现最广泛的应用.应用的领域包括:量子物理.射电天文学.MP3和J ...
- Android ScrollView与ViewPager滑动冲突
前段时间做项目碰到在ScrollView里添加ViewPager,但是发现ViewPager的左右滑动和ScrollView的滑动冲突了,解决这个问题的方法是重写ScrollView. 代码: pub ...