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
Configure JCF messages
by
GordonFindlay
[
Today
at 10:16:19 pm]
Poll: Watches and Display...
by
Martin_fr
[
Today
at 10:15:08 pm]
Dialog ShowModal and an i...
by
d7_2_laz
[
Today
at 09:51:39 pm]
JEDI error but unit compi...
by
Thaddy
[
Today
at 09:30:33 pm]
Understanding and adjusti...
by
circular
[
Today
at 09:09:56 pm]
how to search in rxmemory...
by
rcmz
[
Today
at 08:45:16 pm]
Controlling the behavior ...
by
wp
[
Today
at 08:29:28 pm]
Offical launch of the 1 B...
by
Gustavo 'Gus' Carreno
[
Today
at 08:21:29 pm]
how to change the color f...
by
wp
[
Today
at 07:23:58 pm]
Dynarray initialization p...
by
Thaddy
[
Today
at 06:30:12 pm]
Project fails on run but ...
by
dseligo
[
Today
at 05:30:42 pm]
Zeos components. Had to c...
by
Fantablup
[
Today
at 04:17:13 pm]
[SOLVED] Completion box s...
by
furious programming
[
Today
at 04:12:26 pm]
[LAMW] How to create a ca...
by
af0815
[
Today
at 03:48:16 pm]
Complex package installat...
by
wp
[
Today
at 03:38:16 pm]
A "leisure" question on W...
by
gidesa
[
Today
at 03:17:10 pm]
Curling Pythagoras Tree: ...
by
AlexTP
[
Today
at 03:07:53 pm]
[SOLVED] Combined pointer...
by
CM630
[
Today
at 02:48:41 pm]
Using KeyPress event, how...
by
Zvoni
[
Today
at 02:24:42 pm]
a Unit problem?
by
Thaddy
[
Today
at 01:56:12 pm]
Any upgrades to fpWeb pag...
by
marcov
[
Today
at 12:50:25 pm]
Help needed with Cairo / ...
by
Roland57
[
Today
at 12:25:16 pm]
IDE instability on Raspbe...
by
rvk
[
Today
at 10:37:14 am]
Boleeman's projects are p...
by
AlexTP
[
Today
at 10:18:04 am]
Your best UI design - con...
by
circular
[
Today
at 09:36:11 am]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Convert hex string to ascii? (Read 24041 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: 11857
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: 2223
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