日期:2014-05-19  浏览次数:20722 次

关于字符串参数传递问题
有一C++类TB001:
它里面有一方法:
int   InsertRecord1(int   id,   char*   str1,   char*   str2);


C#函数要调用它!代码如下:

TB001   tb   =   new   TB001();
int   id   =   1;
string   str1   =   "TMD ";
byte[]   buf   =   System.Text.Encoding.ASCII.GetBytes(str1);
sbyte[]   sbuf   =   new   SByte[buf.Length];

for   (int   i   =   0;   i   <   buf.Length;   i++)
{
        sbuf[i]   =   (sbyte)buf[i];
}
tb.InsertRecord1(id,   ref   sbuf[0],   ref   sbuf[0]);     //结果这一行这里失败:

Error         1         The   best   overloaded   method   match   for   'Test.TB001.InsertRecord1(int,   sbyte*,   sbyte*) '   has   some   invalid   arguments         D:\ExtremeDB\Test2\TestApp\Program.cs         115         13         TestApp

Error         2         Argument   '2 ':   cannot   convert   from   'ref   sbyte '   to   'sbyte* '         D:\ExtremeDB\Test2\TestApp\Program.cs         115         38         TestApp

请问我应该怎么写?

------解决方案--------------------
帮顶一下.. 看到沙发... 不顶对不起自己

虽然不会..
------解决方案--------------------
用StringBuilder
------解决方案--------------------
用StringBuilder代替你的数组,这都不会?
------解决方案--------------------
byte[] buf = System.Text.Encoding.ASCII.GetBytes(str1);
sbyte[] sbuf = new SByte[buf.Length];
改为:
StringBuilder buf=new StringBuilder(system.Text.Encoding.ASCII.GetBytes(str1);
StringBuilder sbuf=new StringBuilder(buf.Length.toString());

for (int i = 0; i < buf.Length; i++)
{
sbuf[i] = (sbyte)buf[i];
}
改为:
for(int i=0;i <buf.length;i++)
{
sbuf.append(buf[i]);
}
//记不太清楚了,自己试一下就知道了,