### Author Topic: Convert hex string to ascii?  (Read 11431 times)

#### zolookas

• New member
• Posts: 6
##### Convert hex string to ascii?
« on: December 29, 2007, 09:51:47 pm »
How can i convert hex string to ascii (normal string)?
Like this: '48656C6C6F' -> 'Hello'

#### antonio

• Hero Member
• Posts: 605
##### RE: Convert hex string to ascii?
« Reply #1 on: December 30, 2007, 02:31:02 am »
function IntToHex(Value: Integer; Digits: Integer): string;

#### zolookas

• New member
• Posts: 6
##### RE: Convert hex string to ascii?
« Reply #2 on: January 04, 2008, 04:43:24 pm »
Function HexToStr(s: String): String;
Var i: Integer;
Begin
Result:=''; i:=1;
While i<Length(s) Do Begin
Result:=Result+Chr(StrToIntDef('\$'+Copy(s,i,2),0));
Inc(i,2);
End;
End;

#### apexcol

• New member
• Posts: 42
##### Re: Convert hex string to ascii?
« Reply #3 on: June 11, 2017, 04:01:57 am »
this is an example of using a faster code...
Code: Pascal  [Select]
1. function Hex2Str(const s:String):String;
3.   c:PChar;
4.   h:String[2]='';  {Uses less memory because this is ShortString}
5. begin
6.   Result:='';
7.   for c^ in s do begin
8.     h+=c^;
9.     if h[0]=#2 then begin
10.       Result:=Result+Chr(StrToInt('\$'+h));
11.       h[0]:=#0;  {you can change this by h:=''}
12.     end;
13.   end;
14. end;
15.
I know it's a little late for this answer, but I was not available all this time...

#### apexcol

• New member
• Posts: 42
##### Re: Convert hex string to ascii?
« Reply #4 on: June 11, 2017, 08:56:11 am »
I remembered that the first solution only works for traditional Pascal strings, so they must use a ShortString variable to make it work, but here is a better solution even faster than the other ones...
Code: Pascal  [Select]
1. function Hex2Str2(const s:String):String;
3.   c:Pchar;
4.   h:String[2]='';
5. begin
6.   Result:='';
7.   c:=@s[1];
8.   while c^<>#0 do begin
9.     h+=c^;
10.     inc(c);
11.     if h[0]=#2 then begin
12.       Result:=Result+Chr(StrToInt('\$'+h));
13.       h[0]:=#0;
14.     end;
15.   end;
16. end;
17.
I hope you enjoy it... I tried that with a 1.000.000 length String...
« Last Edit: June 11, 2017, 09:01:34 am by apexcol »

#### Ondrej Pokorny

• Full Member
• Posts: 187
##### Re: Convert hex string to ascii?
« Reply #5 on: June 11, 2017, 10:33:53 am »
FYI: "Result:=Result+C" for strings is not fast at all. Basically you are shooting at your memory manager.

#### fred

• Jr. Member
• Posts: 92
##### Re: Convert hex string to ascii?
« Reply #6 on: June 11, 2017, 11:57:43 am »
Did not test if it's faster, just another way to solve something

Code: Pascal  [Select]
1. function Hex2Str2(const s:String):String;
2. var
3.   c: char;
4.   i, n, h: integer;
5.
6. begin
7.   SetLength(Result, Length(s) div 2);
8.   for i := 1 to Length(s) do begin
9.     c := s[i];
10.     case c of
11.       '0'..'9': n := Ord(c) - Ord('0');
12.       'A'..'F': n := Ord(c) - Ord('A') + 10;
13.       else Exit('Invalid hex string');
14.     end;
15.     if Odd(i)
16.     then h := n
17.     else Result[i div 2] := Chr(h << 4 + n);
18.   end;
19. end;
Started with OmegaSoft Pascal on OS-9/68k , now Lazarus 1.8 RC5 / FPC 3.0.4 on Windows 7

#### wp

• Hero Member
• Posts: 4188
##### Re: Convert hex string to ascii?
« Reply #7 on: June 11, 2017, 01:01:31 pm »
Did not test if it's faster
A factor 10-20.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

#### ASerge

• Hero Member
• Posts: 569
##### Re: Convert hex string to ascii?
« Reply #8 on: June 11, 2017, 04:42:22 pm »
Prefer to use RTL
Code: Pascal  [Select]
1. program Project1;
2.
3. {\$APPTYPE CONSOLE}
4. {\$LONGSTRINGS ON}
5.
6. uses Classes;
7.
8. function HexStrToStr(const HexStr: string): string;
9. var
10.   ResultLen: Integer;
11. begin
12.   ResultLen := Length(HexStr) div 2;
13.   SetLength(Result, ResultLen);
14.   if ResultLen > 0 then
15.     SetLength(Result, HexToBin(Pointer(HexStr), Pointer(Result), ResultLen));
16. end;
17.
18. function StrToHexStr(const S: string): string;
19. var
20.   ResultLen: Integer;
21. begin
22.   ResultLen := Length(S) * 2;
23.   SetLength(Result, ResultLen);
24.   if ResultLen > 0 then
25.     BinToHex(Pointer(S), Pointer(Result), Length(S));
26. end;
27.
28. const
29.   CHello: string = 'Hello world';
30. begin
31.   Writeln(StrToHexStr(CHello));
32.   Writeln(HexStrToStr(StrToHexStr(CHello) + '21'));
34. end.

#### Almir.Bispo

• Jr. Member
• Posts: 64
• CSV Comp DB is the Best NoSQL
##### Re: Convert hex string to ascii?
« Reply #9 on: July 24, 2017, 11:14:59 pm »
Most easy Way is:

Code: Pascal  [Select]
1. { TForm1 }
2. uses strutils;
3. function hex_to_asci(hex:string):string;
4. var i,j:integer;
5.     conc:string;
6.     a:byte;
7. begin
8. for i:= 1 to (length(hex) div (2) ) do
9. begin
10. j:=(i*2)-1;
11. a:=byte( strutils.Hex2Dec(copy(hex, j ,2) ) );
12. conc := conc+ char(a);
13. end;
14. result:= conc;
15. end;
16. //how to use it
17. procedure TForm1.Button1Click(Sender: TObject);
18. begin
19. memo1.text:=hex_to_asci('41424344');
20. end;
21.
22.