如果我不正确,下面的代码用于将字节数组复制到C#中的内存位置:
byte[] byteName = Encoding.ASCII.GetBytes("Hello There");
int positionMemory = getPosition();
Marshal.Copy(byteName, 0, new IntPtr(positionMemory), byteName.length);
Run Code Online (Sandbox Code Playgroud)
如何在本机C++中实现这一点?
提前致谢.
使用指针和memcpy:
void * memcpy ( void * destination, const void * source, size_t num );
Run Code Online (Sandbox Code Playgroud)
假设您要将长度为n的数组A复制到数组B中
memcpy (B, A, n * sizeof(char));
Run Code Online (Sandbox Code Playgroud)
这比C++更多C,字符串类具有可以使用的复制功能.
size_t length;
char buffer[20];
string str ("Test string...");
length=str.copy(buffer,6,5);
buffer[length]='\0';
Run Code Online (Sandbox Code Playgroud)
这是一个具有完整代码的更具体的示例:
#include <stdio.h>
#include <string>
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
string s("Hello World");
char buffer [255];
void * p = buffer; // Or void * p = getPosition()
memcpy(p,s.c_str(),s.length()+1);
cout << s << endl;
cout << buffer << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果您需要更多详细信息,请告诉我们
| 归档时间: |
|
| 查看次数: |
5319 次 |
| 最近记录: |