c++转换C#问题,立马给分
我在翻译C++代码成C#的时候,遇到翻译不过去的情况,以下是C++代码:
int CResponseEvdo::DecodeUCS2( BYTE pdu[], int nInLength,CString& message)
{
if(!message.IsEmpty())
{
message.Empty();
}
for(int i=0;i<nInLength;i++)
{
WCHAR lTemp =pdu[i*2]*256+pdu[i*2+1];
message+=lTemp;
}
return (message.GetLength());
}
我翻译成C#的代码如下:
private string DecodeUCS2(byte[] text, int length)
{
byte[] bytes = new byte[512];
byte[] bBytes = new byte[512];
Buffer.BlockCopy(text, 0, bytes, 0, text.Length);
string message = string.Empty;
if (!string.IsNullOrEmpty(message))
{
message = string.Empty;
}
for (int i = 0; i < length; i++)
{
bBytes[i] = bytes[i * 2] * 256 + bytes[i * 2 + 1];
}
message = System.Text.Encoding.Default.GetString
(bBytes,0,bBytes .Length );
return message;
}
string DecodeUCS2(byte[] text, int length)
{
return Encoding.BigEndianUnicode.GetString(text, 0, length);
}
// 参数:
// value:
// 一个数组。
//
// startIndex:
// value 内的起始位置。
//
// 返回结果:
// 由两个字节构成、从 startIndex 开始的字符。
//
// 异常:
// System.ArgumentException:
// startIndex 等于 value 减 1 的长度。
//
// System.ArgumentNullException:
// value 为 null。
//
// System.ArgumentOutOfRangeException:
// startIndex 小于零或大于 value 减 1 的长度。
public static char ToChar(byte[] value, int startIndex);
[解决办法]
byte[] bBytes
--------------->
short[] bBytes
[解决办法]
.ToString()
然后再ToList()不行吗?
[解决办法]
试一下这样行不行:
private string DecodeUCS2(byte[] text, int length) {
return Encoding.Default.GetString(text, 0, length);
}