delphi代码转C#代码中的问题

agully 2014-09-02 05:13:19
我在对一个delphi的函数进行转换成C#的处理中,运行没问题,但是返回值不一样,delphi的代码是在delphi7下编译的,代码如下:
function Enc(sInput: string): string;stdcall;
Const
AllChar:string='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ';
Var
sOutput :string;
iPin:byte;
A: Boolean;
begin
Result := '';
if sInput = '' Then
sOutput := ''
else
begin
A := False;
if Length(sInput) Mod 2 = 1 then
begin
A := True;
sInput := sInput + '_';
end;
for iPin:=1 to Length(sInput) do
begin
sOutput := sOutput+Char(Ord(sInput[iPin]) + 128);
end;
if A then sOutput := sOutput + '_';
end;
Result := PChar(Trim(sOutput));
end;
,我转换成C#,是这样写的:
public string Enc(string sInput)
{
string AllChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string sOutput;
byte iPin;
Boolean A;
int len = 0;
Int32 K = 0;
sOutput = "";
if (sInput == "") sOutput = "";
else
{
A = false;
len = sInput.Length;
if (len % 2 == 1)
{
A = true;
sInput = sInput + "_";
}
for (iPin = 0; iPin < len; iPin++)
{
K = Convert.ToInt32(sInput[iPin]);
sOutput = sOutput + (char)(Convert.ToInt32(sInput[iPin])+128);
}
if (A == true)
{
sOutput = sOutput + "_";
}
}
return sOutput;
}

运行是没有问题,但是执行结果不一样,在delphi下,字符串“淤釉磐”的执行结果是“SYSTEM”,可是在C#下就不行,delphi7下是采用ANSI码,而C#中是采用UniCode编码,不知道是不是这个问题,但是一直解决不了,期望大能解惑,谢谢
...全文
122 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunny906 2014-09-03
  • 打赏
  • 举报
回复
引用 7 楼 bdmh 的回复:
c#改成如下

        public string Enc(string sInput)
        {
            string AllChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            string sOutput;
            byte iPin;
            Boolean A;
            int len = 0;
            Int32 K = 0;
            sOutput = "";
            byte[] bs = Encoding.Default.GetBytes(sInput);
            byte[] outbs = new byte[bs.Length];
            if (sInput == "") sOutput = "";
            else
            {
                A = false;
                len = bs.Length;
                if (len % 2 == 1)
                {
                    A = true;
                    sInput = sInput + "_";
                }
                for (iPin = 0; iPin < len; iPin++)
                {
                    outbs[iPin] = (byte)(Convert.ToInt32(bs[iPin]) + 128);
                }
            }
            sOutput = Encoding.Default.GetString(outbs);
            return sOutput;
        }
+1 一个汉字在delphi里的length为2,在C#里的长度为1 代码再小改一下

        public string Enc(string sInput)
        {
            string AllChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            string sOutput;
            byte iPin;
            Boolean A;
            int len = 0;
            Int32 K = 0;
            sOutput = "";
            byte[] bs = Encoding.Default.GetBytes(sInput);
            byte[] outbs = new byte[bs.Length];
            if (sInput == "") sOutput = "";
            else
            {
                A = false;
                len = bs.Length;
                if (len % 2 == 1)
                {
                    A = true;
                    sInput = sInput + "_";
                    bs = Encoding.Default.GetBytes(sInput);
                    len = bs.Length;
                }

                for (iPin = 0; iPin < len; iPin++)
                {
                    outbs[iPin] = (byte)(Convert.ToInt32(bs[iPin]) + 128);
                }
            }
            sOutput = Encoding.Default.GetString(outbs);
            return sOutput;
        }
bdmh 2014-09-03
  • 打赏
  • 举报
回复
c#改成如下

        public string Enc(string sInput)
        {
            string AllChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            string sOutput;
            byte iPin;
            Boolean A;
            int len = 0;
            Int32 K = 0;
            sOutput = "";
            byte[] bs = Encoding.Default.GetBytes(sInput);
            byte[] outbs = new byte[bs.Length];
            if (sInput == "") sOutput = "";
            else
            {
                A = false;
                len = bs.Length;
                if (len % 2 == 1)
                {
                    A = true;
                    sInput = sInput + "_";
                }
                for (iPin = 0; iPin < len; iPin++)
                {
                    outbs[iPin] = (byte)(Convert.ToInt32(bs[iPin]) + 128);
                }
            }
            sOutput = Encoding.Default.GetString(outbs);
            return sOutput;
        }
bdmh 2014-09-03
  • 打赏
  • 举报
回复
告诉我你的输入是什么,输出 应该是什么,我试试
agully 2014-09-03
  • 打赏
  • 举报
回复
引用 4 楼 bdmh 的回复:
是,delphi7中char是单字节,c#是unicode的,你都改用byte试试
都改用byte?麻烦指教下,我C#不是很懂,都是依样画葫芦的,谢谢
bdmh 2014-09-03
  • 打赏
  • 举报
回复
是,delphi7中char是单字节,c#是unicode的,你都改用byte试试
agully 2014-09-03
  • 打赏
  • 举报
回复
引用 2 楼 Cnwanglin 的回复:
AllChar:string 算法没问题吧 allchar没有使用的地方
没问题
Cnwanglin 2014-09-02
  • 打赏
  • 举报
回复
AllChar:string 算法没问题吧 allchar没有使用的地方
Cnwanglin 2014-09-02
  • 打赏
  • 举报
回复
AllChar:string='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 '; 后面有个空格?

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧