复制拷贝 byte数组,应该用哪个API????

lifeixie 2007-04-17 02:47:40
截取一个byte[]中的 任意和byre[]复制拷贝 给另一个 byte[],应该用哪个API????
C#有函数可以实现!!Buffer.BlockCopy

但我的目的是哟啊知道实现这个功能用哪个API函数??!!!
请问应该用哪个???
...全文
1706 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
cn_tigers 2007-07-30
  • 打赏
  • 举报
回复
FileStream FSWrite = new FileStream("10000.bin", FileMode.CreateNew);
BinaryWriter BWrite = new BinaryWriter(FSWrite);
string accno="852100001234567890123456";
//int sequence=buffer.GetValue
byte[] accnobyte = System.Text.Encoding.Default.GetBytes(accno);
System.Array.Copy(accnobyte, 0, buffer, 32704, 24); //替換帳號

看上面的程序用System.Array.Copy,,把buffer的32704位開始的24位換成"852100001234567890123456";
lifeixie 2007-05-08
  • 打赏
  • 举报
回复
!!!!!!!!!!!!!!
lifeixie 2007-04-19
  • 打赏
  • 举报
回复
我记得 memcpy 是VC里的!
lifeixie 2007-04-19
  • 打赏
  • 举报
回复
你把你认为是的那个API改名先,再调用Buffer.BlockCopy看看还能不能copy,就知道了
但我认为没必要调用API,CLR里面C#用unsafe代码就可以搞定吧

上面 C#的 unsafe的代码的速度和 Buffer.BlockCopy比,基本一样速度,甚至没有Buffer.BlockCopy快!!!!! 下面代码好象还如 Buffer.BlockCopy快,一个个指针移动的话,更不如Buffer.BlockCopy快了,何况还是4个4个移动!
public static void BlockCopy(ref byte[] src, int srcIndex, ref byte[] dst, int dstIndex, int count)
{
if (src == null || srcIndex < 0 ||
dst == null || dstIndex < 0 || count < 0)
{
throw new System.ArgumentException();
}

int srcLen = src.Length;
int dstLen = dst.Length;
if (srcLen - srcIndex < count || dstLen - dstIndex < count)
{
throw new System.ArgumentException();
}

// The following fixed statement pins the location of the src and dst objects
// in memory so that they will not be moved by garbage collection.
fixed (byte* pSrc = src, pDst = dst)
{
byte* ps = pSrc;
byte* pd = pDst;

// Loop over the count in blocks of 4 bytes, copying an integer (4 bytes) at a time:
for (int i = 0; i < count / 4; i++)
{
*((int*)pd) = *((int*)ps);
pd += 4;
ps += 4;
}

// Complete the copy by moving any bytes that weren't moved in blocks of 4:
for (int i = 0; i < count % 4; i++)
{
*pd = *ps;
pd++;
ps++;
}
}
}
zhujiechang 2007-04-19
  • 打赏
  • 举报
回复
下面是Buffer.BlockCopy调用的实际C++代码,没有直接API,分32位和64位两种方式调用。
#if defined(_X86_)
//This is a replacement for the memmove intrinsic.
//It performs better than the CRT one and the inline version.
// On WIN64 the CRT implementation of memmove is actually faster than the CLR implementation of m_memmove().
void m_memmove(BYTE* dmem, BYTE* smem, int size)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_COOPERATIVE;
PRECONDITION(CheckPointer(dmem));
PRECONDITION(CheckPointer(smem));
PRECONDITION(size >= 0);
SO_TOLERANT;
}
CONTRACTL_END;

#if defined(_WIN64) || defined(ALIGN_ACCESS)
// Bail out and use the slow version if the destination and the source don't have the same alignment.
if ( ( ((SIZE_T)dmem) & (sizeof(SIZE_T) - 1) ) !=
( ((SIZE_T)smem) & (sizeof(SIZE_T) - 1) ) )
{
memmove(dmem, smem, size);
}
else
#endif // _WIN64 || ALIGN_ACCESS
if (dmem <= smem)
{
// make sure the destination is pointer-aligned
while (( ((SIZE_T)dmem) & (sizeof(SIZE_T) - 1) ) != 0 && size >= (int)(sizeof(SIZE_T) - 1))
{
*dmem++ = *smem++;
size -= 1;
}

// copy 16 bytes at a time
if (size >= 16)
{
size -= 16;
do
{
((DWORD *)dmem)[0] = ((DWORD *)smem)[0];
((DWORD *)dmem)[1] = ((DWORD *)smem)[1];
((DWORD *)dmem)[2] = ((DWORD *)smem)[2];
((DWORD *)dmem)[3] = ((DWORD *)smem)[3];
dmem += 16;
smem += 16;
}
while ((size -= 16) >= 0);
}

#if defined(_WIN64) || defined(ALIGN_ACCESS)
if (!IS_ALIGNED((SIZE_T)dmem, sizeof(SIZE_T)))
{
while (size > 0)
{
*dmem++ = *smem++;
size -= 1;
}
}
else
#endif // _WIN64 || ALIGN_ACCESS
{
// still 8 bytes or more left to copy?
if (size & 8)
{
((DWORD *)dmem)[0] = ((DWORD *)smem)[0];
((DWORD *)dmem)[1] = ((DWORD *)smem)[1];
dmem += 8;
smem += 8;
}

// still 4 bytes or more left to copy?
if (size & 4)
{
((DWORD *)dmem)[0] = ((DWORD *)smem)[0];
dmem += 4;
smem += 4;
}

// still 2 bytes or more left to copy?
if (size & 2)
{
((WORD *)dmem)[0] = ((WORD *)smem)[0];
dmem += 2;
smem += 2;
}

// still 1 byte left to copy?
if (size & 1)
{
dmem[0] = smem[0];
dmem += 1;
smem += 1;
}
}
}
else
{
smem += size;
dmem += size;

// make sure the destination is pointer-aligned
while (( ((SIZE_T)dmem) & (sizeof(SIZE_T) - 1) ) != 0 && size >= (int)(sizeof(SIZE_T) - 1))
{
*--dmem = *--smem;
size -= 1;
}

// copy 16 bytes at a time
if (size >= 16)
{
size -= 16;
do
{
dmem -= 16;
smem -= 16;
((DWORD *)dmem)[3] = ((DWORD *)smem)[3];
((DWORD *)dmem)[2] = ((DWORD *)smem)[2];
((DWORD *)dmem)[1] = ((DWORD *)smem)[1];
((DWORD *)dmem)[0] = ((DWORD *)smem)[0];
}
while ((size -= 16) >= 0);
}

#if defined(_WIN64) || defined(ALIGN_ACCESS)
if (!IS_ALIGNED((SIZE_T)dmem, sizeof(SIZE_T)))
{
while (size > 0)
{
*--dmem = *--smem;
size -= 1;
}
}
else
#endif // _WIN64 || ALIGN_ACCESS
{
// still 8 bytes or more left to copy?
if (size & 8)
{
dmem -= 8;
smem -= 8;
((DWORD *)dmem)[1] = ((DWORD *)smem)[1];
((DWORD *)dmem)[0] = ((DWORD *)smem)[0];
}

// still 4 bytes or more left to copy?
if (size & 4)
{
dmem -= 4;
smem -= 4;
((DWORD *)dmem)[0] = ((DWORD *)smem)[0];
}

// still 2 bytes or more left to copy?
if (size & 2)
{
dmem -= 2;
smem -= 2;
((WORD *)dmem)[0] = ((WORD *)smem)[0];
}

// still 1 byte left to copy?
if (size & 1)
{
dmem -= 1;
smem -= 1;
dmem[0] = smem[0];
}
}
}
}
#else
#define m_memmove(a, b, c) memmove((a), (b), (c))
#endif // _X86_
兔子-顾问 2007-04-18
  • 打赏
  • 举报
回复
應該不是api。托管對象自然是內存被clr托管,這樣的拷貝工作。應該是clr做的。要轉到用api。需要先fixed。
兔子-顾问 2007-04-18
  • 打赏
  • 举报
回复
也許是,但也許不是,這樣的小功能,調用api微軟自己的clr自己的方法快吧。可能沒有用api。
兔子-顾问 2007-04-18
  • 打赏
  • 举报
回复
哦。要api。是這個:CopyMemory
lifeixie 2007-04-18
  • 打赏
  • 举报
回复
晕!!!
兔子-顾问 2007-04-18
  • 打赏
  • 举报
回复
Array.Copy
lifeixie 2007-04-18
  • 打赏
  • 举报
回复
memcpy(源地址,目标地址,长度)
原地址 byte[] a = {1,2,3,4,5,6,7}
目标地址 byte[] b = new byte[3];

截取 a 中 3,4,5 拷贝到 b


memcpy(源地址,远地址第2下标,远地址第4下标,目标地址,长度)

???
这样怎么写?????
viena 2007-04-18
  • 打赏
  • 举报
回复
你把你认为是的那个API改名先,再调用Buffer.BlockCopy看看还能不能copy,就知道了
但我认为没必要调用API,CLR里面C#用unsafe代码就可以搞定吧
Red_angelX 2007-04-18
  • 打赏
  • 举报
回复
也 刚调了下居然是有 以前有次专门找它解过死都说找不到定义...
Red_angelX 2007-04-18
  • 打赏
  • 举报
回复
我在vc里重来没发现过CopyMemory 都是用RtlMoveMemory
兔子-顾问 2007-04-18
  • 打赏
  • 举报
回复
--------------------------------------------------------------
Red_angelX(八戒) ( ) 信誉:100 Blog 加为好友 2007-04-18 14:45:06 得分: 0


没有CopyMemory这个API 只有Rtl**CopyMemory

memcpy似乎是标准库里面的函数 我寒了...
--------------------------------------------------------------

CopyMemory
The CopyMemory function copies a block of memory from one location to another.

VOID CopyMemory(
PVOID Destination, // pointer to address of copy destination
CONST VOID *Source, // pointer to address of block to copy
DWORD Length // size, in bytes, of block to copy
);

Parameters
Destination
Pointer to the starting address of the copied block's destination.
Source
Pointer to the starting address of the block of memory to copy.
Length
Specifies the size, in bytes, of the block of memory to copy.
Return Values
This function has no return value.

Remarks
If the source and destination blocks overlap, the results are undefined. For overlapped blocks, use the MoveMemory function.

QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Unsupported.
Header: Declared in winbase.h.

See Also
Memory Management Overview, Memory Management Functions, CopyMemoryVlm, FillMemory, FillMemoryVlm, MoveMemory, MoveMemoryVlm, ZeroMemory


Red_angelX 2007-04-18
  • 打赏
  • 举报
回复
[DllImport("Kernel32.dll", EntryPoint="RtlMoveMemory", SetLastError=false)]
static extern void MoveMemory(IntPtr dest, IntPtr src, int size);
Red_angelX 2007-04-18
  • 打赏
  • 举报
回复
没有CopyMemory这个API 只有Rtl**CopyMemory

memcpy似乎是标准库里面的函数 我寒了...
wzq6511 2007-04-18
  • 打赏
  • 举报
回复
mark
兔子-顾问 2007-04-18
  • 打赏
  • 举报
回复
[DllImport("kernel32.dll", EntryPoint="CopyMemory")]
public static extern void CopyMemory (
ref int Destination,
ref int Source,
int Length
);

就是这个。
lifeixie 2007-04-18
  • 打赏
  • 举报
回复
那 请问一下memcpy应该在WINDOWS那个DLL下?
user32.dll和Kernel32.dll 都说没找到入口点????????????
谢谢
加载更多回复(6)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧