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 ...
随机推荐
- MVC中使用HTML Helper类扩展HTML控件
文章摘自:http://www.cnblogs.com/zhangziqiu/archive/2009/03/18/1415005.html MVC在view页面,经常需要用到很多封装好的HTML控件 ...
- PHP 获取网上文件内容
<?php $ch = curl_init("http://game.qq.com/comm-htdocs/js/game_area/moba_server_select.js&quo ...
- Notepad++ 配置java编译环境
仅限于学习java或小的java程序使用.正常写代码还是eclipse吧 ---------------------分割线----------------------------- 1.配置JDK环境 ...
- Spring对事务的处理
1.加入spring-jdbc.jar包 2.配置数据源 3.配置spring事务管理器,spring的事务出现在业务层. <bean id="dataSource" cla ...
- 一些qml资料
qml开发ios应用 http://www.seanyxie.com/qt-qml%E7%A7%BB%E5%8A%A8%E5%BC%80%E5%8F%91%E4%B9%8B%E5%9C%A8ios%E ...
- S1:new操作符
function Shape(type){ this.type = type || "rect"; this.calc = function(){ return "cal ...
- 二模 (12) day1
第一题: 题目大意: 求由N个1,M个0组成的排列的个数,要求在排列的任意一个前缀中,1的个数不少于0的个数.N,M<=5000. 解题过程: 1.看到N,M的范围就明确肯定不会是dp,因为起码 ...
- C# JavaScriptSerializer 解析Json数据(多方法解析Json 三)
准备工作: 1.添加引用System.Web.Extensions, 2..net3.5+版本都有,如果VS2010找不到,在这个文件夹找:C:\Program Files\Reference Ass ...
- IT公司100题-6-根据上排给出十个数,在其下排填出对应的十个数
问题描述: 给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数要求下排每个数都是先前上排那十个数在下排出现的次数.上排的十个数如下:[0,1,2,3,4,5,6,7,8,9] 举一个例子, ...
- checkbox的全选、反选、删除(适配器)
package com.example.adapter; import java.util.List; import com.example.ay.R;import com.example.vo.Fl ...