我正在尝试将以下代码从C++重写为C#,但无法生成相同的输出,在座的任何人能给我一些想法吗?非常感谢!
AnsiString asImg = "0001";
AnsiString asLen;
asLen.SetLength(4);
long lnLen = asImg.Length();
CopyMemory(asLen.c_str(), &lnLen, sizeof(long));发布于 2015-08-08 08:50:03
你上面展示的“道德等价”代码应该是这样的:
string asImg = "0001";
string asLen = asImg;如果你想要在内存访问模式方面更相似的东西,你需要做一些类似的事情:
byte[] asImg = new[] {(byte)'0', (byte)'0', (byte)'0', (byte)'1'};
byte[] asLen = new byte[4];
Array.Copy(asImg, asLen, 4);(但是这个更新的版本真的很奇怪,不是C#程序员应该写的)
如果要将字节字符串0001b解释为整数,可以使用BitConverter
byte[] asImg = new[] {(byte)'0', (byte)'0', (byte)'0', (byte)'1'};
// "int" is probably the same size as "long" in your C example
int asLen = BitConverter.ToInt32(asImg, 0);https://stackoverflow.com/questions/31888459
复制相似问题