7

I developed the following function to convert strings to hex values.

function StrToHex(const S: String): String;
const
  HexDigits: array[0..15] of Char = '0123456789ABCDEF';
var
  I: Integer;
  P1: PChar;
  P2: PChar;
  B: Byte;
begin

  SetLength(Result, Length(S) * 2);
  P1 := @S[1];
  P2 := @Result[1];

  for I := 1 to Length(S) do
  begin
    B := Byte(P1^);
    P2^ := HexDigits[B shr 4];
    Inc(P2);
    P2^ := HexDigits[B and $F];
    Inc(P1);
    Inc(P2);
  end; 

end; 

Now I was wondering whether there is a more efficient way to convert the strings?

5 Answers 5

11

Depending on your Delphi version:

D5-D2007

uses classes;
function String2Hex(const Buffer: Ansistring): string;
begin
  SetLength(result, 2*Length(Buffer));
  BinToHex(@Buffer[1], @result[1], Length(Buffer));
end;

D2009+

uses classes;
function String2Hex(const Buffer: Ansistring): string;
begin
  SetLength(result, 2*Length(Buffer));
  BinToHex(@Buffer[1], PWideChar(@result[1]), Length(Buffer));
end;
4

Try this one

   function String2Hex(const Buffer: Ansistring): string;
   var
     n: Integer;
   begin
     Result := '';
     for n := 1 to Length(Buffer) do
       Result := LowerCase(Result + IntToHex(Ord(Buffer[n]), 2));
   end;
4
  • Great! I just made a performance test and your function is even faster (just a little) than mine. Thanks!
    – oopbase
    Jul 22, 2010 at 11:06
  • 6
    How often do you need to lower-case a string until you can be sure that it really worked?
    – mghie
    Jul 22, 2010 at 11:25
  • 1
    @Forlan07, do your tests again. The code in this sample re-allocates Result once for every char in "Buffer", uses IntToHex to turn numbers into hex... it can't possibly be faster! Your code only allocates memory once, and then does a single for loop over the text to turn it all into hex. Jul 22, 2010 at 12:15
  • @Cosmin Prund: You are right... It seems as if there is a bug in one of my functions where I take the time. I just used a different function to take the time.
    – oopbase
    Jul 22, 2010 at 12:31
3

I know this is a very old topic, but I feel like I kinda need to share my code regarding the question. For years I use my own HexEncode, very similar with Forlan's code up there, but just today I found a faster way to encode Hex. With my old HexEncode, encoding a 180kb binary file took about 50 seconds, while with this function it took up 6 seconds.

function getHexEncode(txt : AnsiString) : AnsiString;
var
  a : integer ;
  st : TStringStream;
  buf : array [0..1] of AnsiChar;
  tmp : ShortString;
begin
  st := TStringStream.Create;
  st.Size := Length(txt)*2;
  st.Position := 0;
  for a:=1 to Length(txt) do
  begin
    tmp := IntToHex(Ord(txt[a]),2);
    buf[0] := tmp[1];
    buf[1] := tmp[2];
    st.Write(buf,2);
  end;
  st.Position := 0;
  Result := st.DataString;
  st.Free;
  //Result := '';  //my old code
  //for a:=1 to Length(txt) do Result := Result+IntToHex(Ord(txt[a]),2);  //my old code
end;
1

It seems good enough, you could always have a byte->2 hex digits lookup table, but that (and similar optimizations) seems like overkill to me in most cases.

0

// StrToInt('$' + MyString); Oops, did not read the question very good...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.