我正在尝试将pascal中的此代码转换为c#.没有编译错误,并且该方法适用于短字符串(4或5个字符).所有方法和函数都是等价的,但我的代码抛出了这个异常:
System.OverflowException:对于字符,值太大或太小.
这是StackTrace:
在C:\ Users\TRS\Documents\Visual Studio 2013\Projects\ConsoleApplication3 \中的ConsoleApplication3.Program.Encrypt(布尔加密,字符串字,Int32 startKey,Int32 multKey,Int32 addKey)中的System.Convert.ToChar(Int32值)中ConsoleApplication3\Program.cs:第29行.
这是Pascal代码:
function TGenericsF.Encrypter(Encrypt: WordBool; Source: AnsiString;
StartKey, MultKey, AddKey: Integer): AnsiString;
{$R-} {$Q-}
var Counter: LongInt;
S: AnsiString;
Ret: AnsiString;
begin
S := Source;
Ret := '';
for Counter := 1 to Length(S) do
begin
if Encrypt then
begin
Ret := Ret + AnsiChar(Ord(S[Counter]) xor (StartKey shr 8));
StartKey := (Ord(Ret[Counter]) + StartKey) * MultKey + AddKey;
end
else
begin
Ret := Ret + AnsiChar(Ord(S[Counter]) xor (StartKey …Run Code Online (Sandbox Code Playgroud)