我将这段C代码改写为pascal,不知对不对,用起来程序老是有点问题,请大家帮忙看看int a(unsigned char *Source,unsigned char *Dest,int Length,bool LeftToRight)
{ int i,j;
unsigned char ch,temp;
j =0;
if(LeftToRight)
{
temp = ((*Source) & 0x80) ;
temp= temp ^ 0x80;
*(Dest+j) = temp;
for(i=0;i<Length;i++)
{
ch = (( (*(Source+i/8)) << (i%8) ) & 0x80) ;
ch= ch ^ 0x80;
if( temp ^ ch )
{
j++;
*(Dest+j)= ch|1;
temp = ch;
}
else{
if( ( (*(Dest+j)) & 0x7f ) ==0x7f )
{
j++;
*(Dest+j)= temp|1;
}
else
*(Dest+j)+=1;
} // if
}// for
}
}
function a(Source: PChar;Dest:PChar;Length:Integer;
LeftToRight:boolean):Integer;
var
i,j:Integer;
ch,temp:Byte;
begin
j:=0;
if(LeftToRight=true) then
begin
temp:=(Byte(Source^) and $80);
temp:=temp xor $80;
(Dest+j)^:=Char(temp);
for i:=0 to Length-1 do
begin
ch:=( Byte((Source+ (i div 8))^) shl (i mod 8)) and $80;
ch:=ch xor $80;
if (temp xor ch)<>0 then
begin
j:=j+1;
(Dest+j)^:=Char(ch or 1);
temp:=ch;
end
else
begin
if (Byte((Dest+j)^) and $7f)=$7f then
begin
j:=j+1;
(Dest+j)^:=Char(temp or 1);
end
else
(Dest+j)^:=Char(Byte((Dest+j)^)+1);
end;
end;
end;
Result:=j+1;
end;
{ int i,j;
unsigned char ch,temp;
j =0;
if(LeftToRight)
{
temp = ((*Source) & 0x80) ;
temp= temp ^ 0x80;
*(Dest+j) = temp;
for(i=0;i<Length;i++)
{
ch = (( (*(Source+i/8)) << (i%8) ) & 0x80) ;
ch= ch ^ 0x80;
if( temp ^ ch )
{
j++;
*(Dest+j)= ch|1;
temp = ch;
}
else{
if( ( (*(Dest+j)) & 0x7f ) ==0x7f )
{
j++;
*(Dest+j)= temp|1;
}
else
*(Dest+j)+=1;
} // if
}// for
}
}
function a(Source: PChar;Dest:PChar;Length:Integer;
LeftToRight:boolean):Integer;
var
i,j:Integer;
ch,temp:Byte;
begin
j:=0;
if(LeftToRight=true) then
begin
temp:=(Byte(Source^) and $80);
temp:=temp xor $80;
(Dest+j)^:=Char(temp);
for i:=0 to Length-1 do
begin
ch:=( Byte((Source+ (i div 8))^) shl (i mod 8)) and $80;
ch:=ch xor $80;
if (temp xor ch)<>0 then
begin
j:=j+1;
(Dest+j)^:=Char(ch or 1);
temp:=ch;
end
else
begin
if (Byte((Dest+j)^) and $7f)=$7f then
begin
j:=j+1;
(Dest+j)^:=Char(temp or 1);
end
else
(Dest+j)^:=Char(Byte((Dest+j)^)+1);
end;
end;
end;
Result:=j+1;
end;
解决方案 »
- <nodename>xxxx</nodename> 如何获取这个结点的名称
- 请问,在Rect(0,0,2000,2000)内的图元如何在Rect(200,200,500,500)的区间内显示
- 这个程序该怎么做 ?delphi要解析远程unix主机中的文件 ?
- 请问斑竹~
- XP效果如何实现?
- 请教如何编程获得机器的gateway,100分啊
- delphi 的例子中有一个聊天程序.可是不支持三方或更多人一起聊.请问怎么改啊.谢谢了.
- mainmenu上的菜单为什么不能调用其它窗体
- 邪门的更新资源-UpdateResource
- 急!急!急!怎么能是dbgrid的下拉菜单自动显示呢
- 怎么限制stringgrid里某列单元格的输入长度
- 求一个带CHECKBOX的Treeview的源程序
1,如果不是要跨语言调用,没有必要用PChar,改成String;
2,object Pascal里不能象C里那样把数组当作一个指针看待,你的代码里凡是Source^和Dest^都改为Source[0]、Dest[0],凡是(Source+i)^都改成Source[i];
3,其实你的函数里没有进行什么字符串操作,那不如改成这样
function a(Source,Dest: array of byte; length:Integer;
LeftToRight:boolean):Integer;
省得你转换来转换去还容易出错。
另外想问一下,我试了,pascal中Char类型类似于c的unsigned char ,PChar类型可以象C中的字符数组一样来访问。
这个问题我劝你还是直接传入array of byte再试试,有可能是Char和Byte来回转换造成的不一致你可以简单的试一下,把一个Char强制转换成Byte位操作后再转回Char,看看是否回出现问题。