Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Using the Lazarus IDE
»
General
»
Convert hex string to ascii?
Free Pascal
Website
Downloads
Wiki
Documentation
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Documentation (RTL/FCL/LCL)
Bugtracker
CCR Bugs
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
Forum Rules
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
FPC Unleashed (inline var...
by
440bx
[
Today
at 08:38:11 pm]
Ann: Deinline: a de-inlin...
by
Thaddy
[
Today
at 06:50:56 pm]
overloading issues fpc3.2...
by
Thaddy
[
Today
at 06:18:43 pm]
Translate .lfm file in a ...
by
jcmontherock
[
Today
at 05:14:17 pm]
Can I get the position an...
by
CM630
[
Today
at 03:36:42 pm]
Seeking advice on setting...
by
schuler
[
Today
at 03:36:28 pm]
AI assisted translation o...
by
schuler
[
Today
at 03:27:56 pm]
Ann: DeCoperators
by
DomingoGP
[
Today
at 03:23:39 pm]
Error: Compilation raised...
by
marcov
[
Today
at 02:44:06 pm]
TLazSerial : serial port ...
by
CM630
[
Today
at 09:50:34 am]
I hope FreePascal can sup...
by
xiyi0616
[
Today
at 05:49:05 am]
[Solved] Help needed comp...
by
landolfi
[
Today
at 02:45:10 am]
Delimited text - how find...
by
Remy Lebeau
[
Today
at 02:26:15 am]
DataPort or Synpase stat...
by
eldonfsr
[April 16, 2026, 11:32:18 pm]
[FPC 3.2.4, Windows] PTC ...
by
Fred vS
[April 16, 2026, 08:26:24 pm]
Cannot build Units for Si...
by
NormanDunbar
[April 16, 2026, 07:39:07 pm]
IDE: Property editor does...
by
dsiders
[April 16, 2026, 07:04:22 pm]
What is wrong with this c...
by
OH1KH
[April 16, 2026, 04:32:48 pm]
[work arounded]Naming dae...
by
LeP
[April 16, 2026, 03:36:10 pm]
Lazarus Bugfix Release 4....
by
Jonax
[April 16, 2026, 03:06:25 pm]
CudaText editor (written ...
by
AlexTP
[April 16, 2026, 10:43:12 am]
C header function with on...
by
440bx
[April 16, 2026, 09:13:38 am]
(Solved)TUpDown KeyDown ...
by
LeP
[April 15, 2026, 11:21:09 pm]
Zipper: Addition of mult...
by
marcov
[April 15, 2026, 10:35:15 pm]
GTK3: FillRect() mispaint...
by
baldzhang
[April 15, 2026, 06:32:52 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Convert hex string to ascii? (Read 26034 times)
zolookas
Newbie
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'
Logged
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;
Logged
LazarusBrasil.Org
zolookas
Newbie
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;
Logged
apexcol
Jr. Member
Posts: 54
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]
[+]
[-]
function
Hex2Str
(
const
s
:
String
)
:
String
;
var
{comments to edgarrod71@gmail.com}
c
:
PChar
;
h
:
String
[
2
]
=
''
;
{Uses less memory because this is ShortString}
begin
Result
:
=
''
;
for
c
^
in
s
do
begin
h
+=
c
^
;
if
h
[
0
]
=
#2
then
begin
Result
:
=
Result
+
Chr
(
StrToInt
(
'$'
+
h
)
)
;
h
[
0
]
:
=
#0
;
{you can change this by h:=''}
end
;
end
;
end
;
I know it's a little late for this answer, but I was not available all this time...
Logged
apexcol
Jr. Member
Posts: 54
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]
[+]
[-]
function
Hex2Str2
(
const
s
:
String
)
:
String
;
var
{comments to edgarrod71@gmail.com}
c
:
Pchar
;
h
:
String
[
2
]
=
''
;
begin
Result
:
=
''
;
c
:
=
@
s
[
1
]
;
while
c
^
<>
#0
do
begin
h
+=
c
^
;
inc
(
c
)
;
if
h
[
0
]
=
#2
then
begin
Result
:
=
Result
+
Chr
(
StrToInt
(
'$'
+
h
)
)
;
h
[
0
]
:
=
#0
;
end
;
end
;
end
;
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
»
Logged
Ondrej Pokorny
Full Member
Posts: 220
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.
Logged
fred
Full Member
Posts: 208
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]
[+]
[-]
function
Hex2Str2
(
const
s
:
String
)
:
String
;
var
c
:
char
;
i
,
n
,
h
:
integer
;
begin
SetLength
(
Result
,
Length
(
s
)
div
2
)
;
for
i
:
=
1
to
Length
(
s
)
do
begin
c
:
=
s
[
i
]
;
case
c
of
'0'
..
'9'
:
n
:
=
Ord
(
c
)
-
Ord
(
'0'
)
;
'A'
..
'F'
:
n
:
=
Ord
(
c
)
-
Ord
(
'A'
)
+
10
;
else
Exit
(
'Invalid hex string'
)
;
end
;
if
Odd
(
i
)
then
h
:
=
n
else
Result
[
i
div
2
]
:
=
Chr
(
h <<
4
+
n
)
;
end
;
end
;
Logged
wp
Hero Member
Posts: 13487
Re: Convert hex string to ascii?
«
Reply #7 on:
June 11, 2017, 01:01:31 pm »
Quote from: fred on June 11, 2017, 11:57:43 am
Did not test if it's faster
A factor 10-20.
Logged
ASerge
Hero Member
Posts: 2481
Re: Convert hex string to ascii?
«
Reply #8 on:
June 11, 2017, 04:42:22 pm »
Prefer to use RTL
Code: Pascal
[Select]
[+]
[-]
program
Project1
;
{$APPTYPE CONSOLE}
{$LONGSTRINGS ON}
uses
Classes
;
function
HexStrToStr
(
const
HexStr
:
string
)
:
string
;
var
ResultLen
:
Integer
;
begin
ResultLen
:
=
Length
(
HexStr
)
div
2
;
SetLength
(
Result
,
ResultLen
)
;
if
ResultLen >
0
then
SetLength
(
Result
,
HexToBin
(
Pointer
(
HexStr
)
,
Pointer
(
Result
)
,
ResultLen
)
)
;
end
;
function
StrToHexStr
(
const
S
:
string
)
:
string
;
var
ResultLen
:
Integer
;
begin
ResultLen
:
=
Length
(
S
)
*
2
;
SetLength
(
Result
,
ResultLen
)
;
if
ResultLen >
0
then
BinToHex
(
Pointer
(
S
)
,
Pointer
(
Result
)
,
Length
(
S
)
)
;
end
;
const
CHello
:
string
=
'Hello world'
;
begin
Writeln
(
StrToHexStr
(
CHello
)
)
;
Writeln
(
HexStrToStr
(
StrToHexStr
(
CHello
)
+
'21'
)
)
;
ReadLn
;
end
.
Logged
Almir.Bispo
Jr. Member
Posts: 94
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]
[+]
[-]
{ TForm1 }
uses
strutils
;
function
hex_to_asci
(
hex
:
string
)
:
string
;
var
i
,
j
:
integer
;
conc
:
string
;
a
:
byte
;
begin
for
i
:
=
1
to
(
length
(
hex
)
div
(
2
)
)
do
begin
j
:
=
(
i
*
2
)
-
1
;
a
:
=
byte
(
strutils
.
Hex2Dec
(
copy
(
hex
,
j
,
2
)
)
)
;
conc
:
=
conc
+
char
(
a
)
;
end
;
result
:
=
conc
;
end
;
//how to use it
procedure
TForm1
.
Button1Click
(
Sender
:
TObject
)
;
begin
memo1
.
text
:
=
hex_to_asci
(
'41424344'
)
;
end
;
Visit my project:
http://adltecnologia.blogspot.com.br
«
Last Edit: July 25, 2017, 12:14:52 am by Almir.Bispo
»
Logged
CSV Comp DB Developer {Pascal Lover}
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Using the Lazarus IDE
»
General
»
Convert hex string to ascii?
TinyPortal
© 2005-2018