delphi - 字节绝对字符串数组:为64位Windows编译时内容不正确

标签 delphi 64-bit delphi-xe5

如果我为64位Windows编译,则我的字节数组没有正确的输入值。
如果我为x32-Windows编译此过程,则值是正确的。

谁能帮我?

procedure doAnything(AText: String); //for example "<xml><s name="hello"/></xml>"
var
  myArray:array of Byte absolute AText;
begin
  ... (* myArray for x32: correct Length and Values (60, 0, 120, 0, 109, 0, ...) *)
  ... (* myArray for x64: Length: 2 (60, 0) *)
end

最佳答案

如果要以原始字节的形式访问String的字符数据,则必须使用类型转换,请勿使用absolute,因为String的内存布局与动态数组不兼容,正如其他人向您指出的那样:

procedure doAnything(AText: String);
var
  myBytes: PByte;
  myBytesLen: Integer;
begin
  myBytes := PByte(PChar(AText));

  myBytesLen := ByteLength(AText);
  // or: myBytesLen := Length(AText) * SizeOf(Char);

  // use myBytes up to myBytesLen as needed...
end;


如果您真的想使用absolute,则必须像这样使用它:

procedure doAnything(AText: String);
var
  myChars: PChar;
  myBytes: PByte absolute myChars;
  myBytesLen: Integer;
begin
  myChars := PChar(AText);

  myBytesLen := ByteLength(AText);
  // or: myBytesLen := Length(AText) * SizeOf(Char);

  // use myBytes up to myBytesLen as needed...
end;

关于delphi - 字节绝对字符串数组:为64位Windows编译时内容不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23743769/

相关文章:

delphi - 在 Delphi 10.2 中写入 MemoryStream 有何变化?

delphi - 如何编写可在MS Word/Excel/Powerpoint中使用的Delphi ActiveX控件?

windows - WiX Burn Bootstrapper - 无法检测注册表项

excel用$返回工作表名

delphi - 我得到 'Variable x inaccessible here due to optimization'

windows - 如何使用来自 Windows 的 python 脚本在 cygwin 中运行程序?

assembly - MOV moffs32 在 64 位模式下对地址进行符号或零扩展?

android - 如何使用 FireMonkey for Delphi XE5 检测/处理屏幕旋转

smtp - 使用Indy smtp时如何避免 "message rejected with no message-id"

Android WebBrowser 向下滚动页面