Can anyone see where the following routine is going wrong, it is part of
an encryption utility I am putting together where I need to convert an
array of bytes into a string representation, I'm using 2 character hex
strings which keeps it readable and suits the mechanism I am using.

I have this routine I found on the net (www.xploiter.com) which looked
like it would work but it keeps raising access violations:

procedure BytesToHexStr(var hHexStr: String; pbyteArray: PByte;
InputLength: WORD); 
Const 
  HexChars : Array[0..15] of Char = '0123456789ABCDEF'; 
var 
  i, j: WORD; 
begin 
  SetLength(hHexStr, (InputLength * 2)); 
  FillChar(hHexStr, sizeof(hHexStr), #0); 
  
  j := 1; 
  for i := 1 to InputLength do begin 
    hHexStr[j] := Char(HexChars[pbyteArray^ shr 4]); inc(j); 
    hHexStr[j] := Char(HexChars[pbyteArray^ and 15]); inc(j); 
    inc(pbyteArray); 
  end; 
end; 

The FillChar statement causes problems but even after removing that I
still hit AVs when I later try to FreeMem() the original ByteArray I
pass into the function, which makes me think the hHexStr is corrupting
memory somehow.

I therefore scrapped this approach and created the following which is
using format to hopefully speed up the string concatenation, but I
wonder if this is a much slower approach?

procedure ByteToHex(pByteArray: PBYTE; var HexStr: string; BufLength:
Integer);
var
  i: integer;
begin
  HexStr := '';

  for i := 0 to BufLength do
  begin
    HexStr := format('%s%.2x', [HexStr, pByteArray^]);
    inc(pByteArray);
  end;
end;

It could be that neither method is that good. Initially this will only
be dealing with small chunks of data anyway but overtime there is a
possibility it will be usurped to work with larger traffic.


Michael Darling
Solutions Analyst/Developer
ROOM Solutions Ltd
mailto:[EMAIL PROTECTED]
*******************************************************************
CONFIDENTIALITY NOTICE/DISCLAIMER
This email and any attachments are confidential, protected by 
copyright/intellectual property rights and may be legally privileged. The 
information transmitted is intended only for the person or entity to which it 
is addressed.  If you are not the intended recipient, dissemination or copying 
of this email is prohibited.
If you have received this in error, please notify us by forwarding this email 
to the following address: [EMAIL PROTECTED] , and then delete the email 
completely from your system.
This email and any attachments have been scanned for computer viruses by a 
market leading anti-virus system. However, it is the responsibility of the 
recipient to conduct its own security measures. No responsibility is accepted 
by ROOM Solutions Limited for loss or damage arising from the receipt or use of 
this email and any attachments.
No responsibility is accepted by ROOM Solutions Limited for personal emails.
ROOM Solutions Ltd, http://www.roomsolutions.net
********************************************************************

_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to