谁能把以下c#代码改为delphi啊????

wedical 2006-12-28 10:59:47
public static UInt32[] ToUInt32Array(string input, Boolean IncludeLength)
{

Byte[] Data=System.Text.Encoding.UTF8.GetBytes(input);
Int32 n = (((Data.Length & 3) == 0) ? (Data.Length >> 2) : ((Data.Length >> 2) + 1));
UInt32[] Result;
if (IncludeLength)
{
Result = new UInt32[n + 1];
Result[n] = (UInt32)Data.Length;
}
else
{
Result = new UInt32[n];
}
n = Data.Length;
for (Int32 i = 0; i < n; i++)
{
Result[i >> 2] |= (UInt32)Data[i] << ((i & 3) << 3);
}
return Result;
}
...全文
152 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿发伯 2006-12-28
  • 打赏
  • 举报
回复
用Delphi写了一个类似函数,不知正确与否。返回值用完后必须用FreeMem释放,因为Delphi没有自动回收内存机制。

function ToUInt32Array(input: string; IncludeLength: Boolean): PLongWord;
type
UWArray = array of LongWord;
var
n, i: Integer;
begin
n := (Length(input) + 3) shr 2;
if IncludeLength then
begin
GetMem(Result, (n + 1) * Sizeof(LongWord));
UWArray(Result)[n] := Length(input);
end else
GetMem(Result, n * Sizeof(LongWord));
n := Length(input) - 1;
for i := 0 to n do
UWArray(Result)[I shr 2] := UWArray(Result)[I shr 2] or ((LongWord(Ord(input[i + 1])) shl ((i and 3) shl 3)));
end;
阿发伯 2006-12-28
  • 打赏
  • 举报
回复
上面的返回值是指针,使用起来可能麻烦,而且用毕还得释放,修改了一下,直接用动态数组,比较符合原函数意。

type
UWArray = array of LongWord;

function ToUInt32Array(input: string; IncludeLength: Boolean): UWArray;
var
n, i: Integer;
begin
n := (Length(input) + 3) shr 2;
if IncludeLength then
begin
SetLength(Result, n + 1);
Result[n] := Length(input);
end else
SetLength(Result, n);
n := Length(input) - 1;
for i := 0 to n do
Result[I shr 2] :=Result[I shr 2] or ((LongWord(Ord(input[i + 1])) shl ((i and 3) shl 3)));
end;

16,749

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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