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
IRC channel
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
Using KeyPress event, how...
by
Zvoni
[
Today
at 09:13:47 am]
STARS with line border st...
by
Boleeman
[
Today
at 09:13:29 am]
JEDI error but unit compi...
by
DomingoGP
[
Today
at 08:28:17 am]
Configure JCF messages
by
DomingoGP
[
Today
at 08:19:10 am]
Any upgrades to fpWeb pag...
by
AlexTP
[
Today
at 08:10:55 am]
Zeos components. Had to c...
by
dbannon
[
Today
at 07:26:11 am]
IDE instability on Raspbe...
by
rvk
[
Today
at 07:08:49 am]
A button require administ...
by
Fibonacci
[
Today
at 05:57:21 am]
How to detect one externa...
by
d2010
[
Today
at 12:24:32 am]
Center Lost Window featur...
by
Tony Stone
[
Today
at 12:06:16 am]
Complex package installat...
by
wp
[March 27, 2024, 10:47:45 pm]
Controlling the behavior ...
by
wp
[March 27, 2024, 10:43:40 pm]
A "leisure" question on W...
by
EventHorizon
[March 27, 2024, 10:18:38 pm]
need advice about the fol...
by
jamie
[March 27, 2024, 09:56:39 pm]
[SOLVED] Completion box s...
by
Martin_fr
[March 27, 2024, 08:44:09 pm]
Dialog ShowModal and an i...
by
d7_2_laz
[March 27, 2024, 08:25:18 pm]
Offical launch of the 1 B...
by
Gustavo 'Gus' Carreno
[March 27, 2024, 07:41:48 pm]
Your best UI design - con...
by
hrayon
[March 27, 2024, 06:42:28 pm]
Understanding and adjusti...
by
ArchChem
[March 27, 2024, 06:04:25 pm]
Problem setting length of...
by
Remy Lebeau
[March 27, 2024, 05:41:10 pm]
TDBLookupcombobox seems u...
by
Fantablup
[March 27, 2024, 05:18:04 pm]
Poll: Watches and Display...
by
Martin_fr
[March 27, 2024, 03:58:57 pm]
Help needed with Cairo / ...
by
AL
[March 27, 2024, 02:35:10 pm]
I encountered onDatachang...
by
Fantablup
[March 27, 2024, 02:34:08 pm]
Code folding tree — highl...
by
Martin_fr
[March 27, 2024, 01:58:19 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Convert hex string to ascii? (Read 24037 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: 201
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: 11853
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: 2222
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: 91
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