问题描述
我有一些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
推荐答案
到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
我甚至不确定我是否理解为什么在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
这篇关于C#中的memcpy char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!