简体   繁体   English

德尔福5至2010年

[英]Delphi 5 to 2010

I used same function ( OneWayEncrypt(edit1.Text) ) in Delphi 5 and 2010. 我在Delphi 5和2010中使用了相同的函数( OneWayEncrypt(edit1.Text) )。
Why the results are different? 为什么结果不同? (Or how can I give the same results from Delphi 2010?) (或者我如何从Delphi 2010中获得相同的结果?)

uses Sysutils, Windows, Dialogs, classes;

function OneWayEncrypt(AStr: string): string;
PROCEDURE CalcCRC32 (p:  pointer; ByteCount:  DWORD; VAR CRCvalue:  DWORD);

implementation

const 
  table:  ARRAY[0..255] OF DWORD = 
  (
    //table consts are here
  );

PROCEDURE CalcCRC32(p: pointer; ByteCount: DWORD; VAR CRCvalue: DWORD);
VAR
  i: DWORD;
  q: ^Byte;
BEGIN
  q := p;
  FOR i := 0 TO ByteCount - 1 DO
  BEGIN
    CRCvalue := (CRCvalue SHR 8) XOR table[q^ XOR (CRCvalue AND $000000FF)];
    INC(q);
  END
END;

function OneWayEncrypt(AStr: string): string;
var
  dwCrc: DWORD;
  s: string;
begin
  dwCrc := $FFFFFFFF; 
  s := 'X' + AStr + '7F';
  CalcCRC32(Addr(s[1]), Length(s), dwCrc);
  result := IntToHex(dwCrc, 8);
end;

Are you aware that string refers to a Unicode string in D2010, while it refers to AnsiString in versions < D2009? 您是否知道string是指D2010中的Unicode字符串,而它是指版本<D2009中的AnsiString? That should be the source of your problem. 那应该是你问题的根源。

So you have two choices: 所以你有两个选择:

  • You could replace all appearances of string with AnsiString . 你可以用AnsiString替换所有string外观。 This should give you the same results as in D5, of course without Unicode support 这应该会给你与D5相同的结果,当然没有Unicode支持
  • You could refactor your code. 你可以重构你的代码。 I guess that the pointer-"hacking" is the crucial part here. 我想指针 - “黑客”是这里的关键部分。 But I have to admit, I didn't take the time to fully understand the code ;-) (It could very well be that your code can't be used with Unicode anyways, due to the 255 consts = ISO8859?) 但我不得不承认,我没有花时间完全理解代码;-)(很可能你的代码不能用于Unicode,因为255 consts = ISO8859?)

D2010 (and D2009) use Unicode strings (widestrings), so the character size is different (bytes). D2010(和D2009)使用Unicode字符串(宽带),因此字符大小不同(字节)。 Try switching all references of string to AnsiString. 尝试将字符串的所有引用切换为AnsiString。

Minimal port, one line change: 最小端口,一行更改:

  // old code:
  CalcCRC32(Addr(s[1]), Length(s), dwCrc);

  // delphi 2010 code:
  CalcCRC32( PAnsiChar(AnsiString(s)), Length(s), dwCrc);

Please be aware that any unicode content in the unicode "String" will be lost, but any ANSI (AZ, 1,3,4, you know) codepoints you used before, for example "Hello", should work just like before. 请注意,unicode“String”中的任何unicode内容都将丢失,但您之前使用的任何ANSI(AZ,1,3,4,您知道)代码点(例如“Hello”)应该像以前一样工作。 Since this is a CRC32 algorithm, it could do a CRC32 on a UTF8 encoding of the string too, easily. 由于这是一个CRC32算法,它也可以很容易地对字符串的UTF8编码执行CRC32。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM