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 判断目录是否有写入权限

Delphi 新建文件 复制文件 重命名文件名时 文件存在自动重命名

Delphi 在windows右键菜单中加上关联

Delphi 拷贝文件夹内所有内容

Delphi dateutils.ispm - 判断时间是否是下午

Delphi dbexpress的upwherekeyonly的使用注意事项

Delphi xe 版本 示例原码下载

Delphi fmx检查应用程序状态更改

Delphi xe5做一个android网页框架

Delphi 过滤字符串特殊符号的函数

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



打赏

取消

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

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

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

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

评论

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