将unsigned char *buf=NULL转换为Pascal
Translate unsigned char *buf=NULL to Pascal?
我在Borland Delphi工作,我在Borland c++ Builder中有几行代码。我想把这些行翻译成Delphi源代码。
unsigned char *buf=NULL;
buf=new unsigned char[SPS*2];
for (i=0; i<SPS*2; i++)
buf[i]=2;
……
answers=buf[2];
我想用这个buf赋一个PCHar值;
a:PCHar;
a:=buf.
事实上,在:
unsigned char *buf=NULL;
buf=new unsigned char[SPS*2];
第一个赋值*buf=NULL
可以翻译为buf := nil
,但它是纯粹的死代码,因为buf
指针的内容立即被new
函数覆盖。
所以你的C代码可以翻译成这样:
var buf: PAnsiChar;
i: integer;
begin
Getmem(buf,SPS*2);
for i := 0 to SPS*2-1 do
buf[i] := #2;
...
Freemem(buf);
end;
一个更符合delphi习惯的版本可能是:
var buf: array of AnsiChar;
i: integer;
begin
SetLength(buf,SPS*2);
for i := 0 to high(buf) do
buf[i] := #2;
...
// no need to free buf[] memory (it is done by the compiler)
end;
或直接:
var buf: array of AnsiChar;
i: integer;
begin
SetLength(buf,SPS*2);
fillchar(buf[0],SPS*2,2);
...
// no need to free buf[] memory (it is done by the compiler)
end;
也许像这样:
var
buf: array of AnsiChar;
a: PAnsiChar;
...
SetLength(buf, SPS*2);
FillChar(buf[0], Length(buf), 2);
a := @buf[0];
不知道answers
是什么,但是,假设它是你的c++代码中的char
,那么你会这样写:
var
answers: AnsiChar;
...
answers := buf[2];
相关文章:
- 防止主数据类型C++的隐式转换
- 将以 null 结尾的字节字符串转换为原始字符串文本
- 将位数组转换为 HBITMAP 后 bmBits 的 NULL 指针
- 将 NULL 转换为长不是模棱两可吗?
- 在构造函数处将类对象强制转换为接口始终返回 NULL
- 无法将模板参数null转换为void*
- 在Linux/GCC下,将NULL指针访问转换为C++异常
- C++将 NULL 和非 NULL 值转换为指针
- 返回 NULL,但收到错误 C2440:'return':无法从'int'转换为'const &'
- QImage转换返回null
- 为什么当我将非 NULL 指针传递给方法时,它们似乎会转换为 NULL?(C++)
- 将 NULL/nullptr 转换为 std::function<>& 时失败
- C++将时间 (NULL) 转换为本地时间的函数
- Delphi - 从 dll 调用转换(读取)C++ NULL 终止数组
- C++将文本文件写入字符串并转换为double,会得到null char或0
- srand(time(null))导致编译器警告:隐式转换会丢失整数精度
- atoi中的Null将值转换为0
- 将unsigned char *buf=NULL转换为Pascal
- 可以安全地将预先存在的类型转换NULL的返回与较新的nullptr进行比较吗
- 为什么指向 NULL 字符的指针没有转换为 false