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
Lazarus for Windows on aa...
by
dbannon
[
Today
at 07:02:55 am]
BUG REPORT: Unable to gen...
by
dbannon
[
Today
at 06:29:49 am]
Database standards OR Am ...
by
cdbc
[
Today
at 06:24:05 am]
Offical launch of the 1 B...
by
Gustavo 'Gus' Carreno
[
Today
at 12:50:46 am]
what to do if my target M...
by
Laksen
[
Today
at 12:49:12 am]
For-in loop over constant...
by
ASerge
[
Today
at 12:36:09 am]
Converting a string/index...
by
TRon
[April 18, 2024, 11:56:37 pm]
exclude ALL packages from...
by
PascalDragon
[April 18, 2024, 11:33:29 pm]
Demoscene The Champs Crac...
by
KodeZwerg
[April 18, 2024, 11:29:50 pm]
Poll: Watches and Display...
by
440bx
[April 18, 2024, 10:55:14 pm]
How to: create DLL file f...
by
paule32
[April 18, 2024, 10:23:59 pm]
Have anyone develop UEFI ...
by
PascalDragon
[April 18, 2024, 10:14:15 pm]
The weird Rewrite bug
by
PascalDragon
[April 18, 2024, 09:58:53 pm]
Assign (textfile) not com...
by
PascalDragon
[April 18, 2024, 09:51:50 pm]
Question for people who h...
by
PascalDragon
[April 18, 2024, 09:44:32 pm]
Linux Workspaces -- StayO...
by
AmatCoder
[April 18, 2024, 08:34:20 pm]
I just released a commerc...
by
Seenkao
[April 18, 2024, 07:57:21 pm]
Access violation when re-...
by
TRon
[April 18, 2024, 07:37:31 pm]
[SOLVED] assembler name i...
by
TRon
[April 18, 2024, 07:26:33 pm]
A fairly simple sound sol...
by
KodeZwerg
[April 18, 2024, 07:01:52 pm]
Step-into the field sette...
by
Martin_fr
[April 18, 2024, 06:54:19 pm]
[Fun Fact] PNG and Micros...
by
Ten_Mile_Hike
[April 18, 2024, 06:24:19 pm]
HTTP/2+HTTP/1.1+WebSocket...
by
iLya2IK
[April 18, 2024, 06:14:30 pm]
Improvement of TFPTimerTh...
by
AlexTP
[April 18, 2024, 05:56:38 pm]
Arabic text, problem on L...
by
nouzi
[April 18, 2024, 05:21:27 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Convert hex string to ascii? (Read 24090 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: 11916
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: 2242
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