C#中的memcpy char * [英] memcpy char* in C#

查看:115
本文介绍了C#中的memcpy char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些c ++代码,我正在转换为C#。我需要转换的是

以下内容:


memcpy(& tmpshort,(pTmpDataIn + 1),2);


这应该将char *的两个字节复制到一个int,然后在其他地方使用

。我无法想出如何接近。


谢谢,


~史蒂夫

I have some c++ code that I am converting to C#. What I need to convert is
the following:

memcpy(&tmpshort, (pTmpDataIn+1), 2);

This should copy two bytes of an char* to an int which then gets used
elsewhere. I am having trouble coming up with how to approach.

Thanks,

~ Steve

推荐答案

2007年12月27日星期四11:45:00 -0800,DatelMonkey99

< da ********* @newsgroups.nospamwrote:
On Thu, 27 Dec 2007 11:45:00 -0800, DatelMonkey99
<da*********@newsgroups.nospamwrote:

我有一些我转换为C#的c ++代码。我需要转换



以下内容:


memcpy(& tmpshort,(pTmpDataIn + 1),2 );


这应该将char *的两个字节复制到一个int,然后在其他地方使用

。我无法想出如何接近。
I have some c++ code that I am converting to C#. What I need to convert
is
the following:

memcpy(&tmpshort, (pTmpDataIn+1), 2);

This should copy two bytes of an char* to an int which then gets used
elsewhere. I am having trouble coming up with how to approach.



到int?或简短?


也许是这样的:


short tmpshort = BitConverter.ToInt16(pTmpDataIn,1);


其中pTmpDataIn是类型byte []。


请注意,在C#中,字符数组与字节数组不同。如果你没有
还没有一些实际的字节数组,或者从

你可以得到一个实际的字节数组,你需要更多

具体关于你的问题。


Pete

To an int? Or a short?

Maybe something like this:

short tmpshort = BitConverter.ToInt16(pTmpDataIn, 1);

where "pTmpDataIn" is type "byte[]".

Note that in C#, a character array isn''t the same as a byte array. If you
don''t already have something that is an actual array of bytes or from
which you can get an actual array of bytes, you''ll need to be more
specific about your question.

Pete


DatelMonkey99写道:
DatelMonkey99 wrote:

我有一些我转换为C#的c ++代码。我需要转换的是

以下内容:


memcpy(& tmpshort,(pTmpDataIn + 1),2);


这应该将char *的两个字节复制到一个int,然后在其他地方使用

。我无法想出如何接近。
I have some c++ code that I am converting to C#. What I need to convert is
the following:

memcpy(&tmpshort, (pTmpDataIn+1), 2);

This should copy two bytes of an char* to an int which then gets used
elsewhere. I am having trouble coming up with how to approach.



我甚至不确定我是否理解为什么在C中使用memcpy。


tmpshort =(短)(( pTmpDataIn [2]<< 8)| pTmpDataIn [1]);


Arne

I am not even sure that I understand why memcpy was used in C.

tmpshort = (short)((pTmpDataIn[2] << 8) | pTmpDataIn[1]);

Arne


嗨Steve,


这将取决于你如何转换char *。从C到C#:如果你是
正在使用一个String,那么你可以使用string.ToCharArray获得一个char数组

并自己加两个字符,只需记住一个字符在C#中是一个unicode。在这种情况下,如果您正在使用P / Invoke函数调用并且正确执行,则非托管内存将自动编组为托管字符串




如果你在C#中使用IntPtr访问非托管内存,那么你

可以使用Marshal.ReadInt16,Marshal.ReadInt32等来阅读非托管内容

记忆。


希望这会有所帮助。

问候,

Walter Wang(wa **** @ online .microsoft.com,删除''在线。'')

Microsoft在线社区支持


============= =====================================

回复帖子时,请回复群组通过你的新闻阅读器

其他人可以从你的问题中学习并从中受益。

==================== ==============================

此帖子提供按现状 ;没有保证,也没有授予任何权利。

Hi Steve,

This will depend on how you''re converting the "char *" from C to C#: if you
are using a String, then you can get a char array using string.ToCharArray
and add up two chars yourself, just remember a char in C# is a unicode. In
this case, the unmanaged memory will be marshalled to managed string
automatically if you are using P/Invoke function calls and do it correctly.

If you are using an IntPtr in C# to access the unmanaged memory, then you
can use Marshal.ReadInt16, Marshal.ReadInt32, etc. to read the unmanaged
memory.

Hope this helps.
Regards,
Walter Wang (wa****@online.microsoft.com, remove ''online.'')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


这篇关于C#中的memcpy char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆