Delphi HexStrToStream


本文整理自网络,侵删。

 

function HexStrToStream(AHexStr: String;AStream: TStream):Boolean;
var
  iLen: Integer;
  Buff: String;
  P1,P2: PChar;
  B: Byte;
begin
  Result := False;
  if Not Assigned(AStream) then Exit;
  iLen := Length(AHexStr);
  if iLen = 0 then Exit;
  if (iLen mod 2) <> 0 then Exit;
  SetString(Buff,Nil,iLen div 2);
  P1 := PChar(AHexStr);
  P2 := PChar(Buff);
  while iLen > 0 do begin
    if P1^ in ['0'..'9'] then
      B := Ord(P1^) - Ord('0')
    else if P1^ in ['A'..'F'] then
      B := Ord(P1^) - Ord('A') + 10
    else if P1^ in ['a','f'] then 
      B := Ord(P1^) - Ord('a') + 10
    else 
      Exit;    
    B := B shl 4;
    Inc(P1);
    Dec(iLen);
    if P1^ in ['0'..'9'] then
      B := B + Ord(P1^) - Ord('0')
    else if P1^ in ['A'..'F'] then
      B := B + Ord(P1^) - Ord('A') + 10
    else if P1^ in ['a','f'] then 
      B := B + Ord(P1^) - Ord('a') + 10
    else 
      Exit; 
    Inc(P1);
    Dec(iLen);
    P2^ := Chr(B);
    Inc(P2);
  end;
  AStream.WriteBuffer(PChar(Buff)^, Length(Buff));
  Result := True;
end;

相关阅读 >>

Delphi中tstringlist分割字符串的用法

Delphi编程之关闭所有qq进程

Delphi 如何将颜色值转换为灰度颜色值?

Delphi经常用到的公共代码(tools.pas)

Delphi延时

Delphi控件安装与删除

Delphi pagecontrol当前页可视

Delphi监视注册表

Delphi Delphi tparallel cleanup needed用法

Delphi shgetfileinfo函数

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...