Discussion:
Converting a hex string to its actual character
(too old to reply)
battles
2012-01-14 00:51:31 UTC
Permalink
I am converting a string of incoming characters that represent a hex
string. A string such as:
616263 which when converted would be abc. I am doing this like this:

var CharStr: string;
const HexStringRep : string = '616263';

CharStr := char(strtoint('$' + HexStringRep[1] + HexStringRep[2])) +
char(strtoint('$' + HexStringRep[3] +
HexStringRep[4])) +
char(strtoint('$' + HexStringRep[5] +
HexStringRep[6]))...

This works ok, but I was wondering if there is a fancier way of doing
this?
Heinrich Wolf
2012-01-14 12:53:36 UTC
Permalink
Post by battles
I am converting a string of incoming characters that represent a hex
var CharStr: string;
const HexStringRep : string = '616263';
CharStr := char(strtoint('$' + HexStringRep[1] + HexStringRep[2])) +
char(strtoint('$' + HexStringRep[3] +
HexStringRep[4])) +
char(strtoint('$' + HexStringRep[5] +
HexStringRep[6]))...
This works ok, but I was wondering if there is a fancier way of doing
this?
Hi,

basically for 8 bit char encoding you do it right. But I guess, for multiple
doing it and for longer strings you use a function and a loop. Furthermore
there are several possible char encodings like widechar, UTF8 or UTF16. In
16 bit char encoding you would convert 4 digits to a char. In UTF8 4 digits
might result in a 2-byte-character. The same way UTF16 might result in a
2-word-character.

Heiner
Hans-Peter Diettrich
2012-01-14 16:53:30 UTC
Permalink
Post by Heinrich Wolf
basically for 8 bit char encoding you do it right. But I guess, for
multiple doing it and for longer strings you use a function and a loop.
Right.
Post by Heinrich Wolf
Furthermore there are several possible char encodings like widechar,
UTF8 or UTF16.
That depends on the encoding function, which must be known. For every
encoding an according encoder and decoder must be implemented.

DoDi
Jamie
2012-01-15 18:55:56 UTC
Permalink
Post by battles
I am converting a string of incoming characters that represent a hex
var CharStr: string;
const HexStringRep : string = '616263';
CharStr := char(strtoint('$' + HexStringRep[1] + HexStringRep[2])) +
char(strtoint('$' + HexStringRep[3] +
HexStringRep[4])) +
char(strtoint('$' + HexStringRep[5] +
HexStringRep[6]))...
This works ok, but I was wondering if there is a fancier way of doing
this?
ok, for some reason I find that hard to believe that the results you're
getting, seeing that you are type casting it to a CHAR would even yield
a proper string.

What is wrong with this.
CharStr := IntTostr('$'+HexStringRep); ?

Simply build a string with the incoming stream of data and when you
reach the delimiter, finalize the building of that string and pass it on
to the next step..

Some where in code land.

MyHExstream:= '';

-- some where else in stream land ----

If not UpCase(IncomingChar) in ['0'..'9','A'..'F'] THen
MyHexstream:=MyHexStream+InComingChar else
Begin
CharStr := IntToStr('$'+MyHExStream);
MyHexStreamm := ''; // Clear for next run.
// Do some other code, etc...
End;

Jamie
Hans-Peter Diettrich
2012-01-16 06:42:47 UTC
Permalink
Post by Jamie
What is wrong with this.
CharStr := IntTostr('$'+HexStringRep); ?
The argument must be a number, but actually is a string.

DoDi
Jamie
2012-01-16 12:39:10 UTC
Permalink
Post by Hans-Peter Diettrich
Post by Jamie
What is wrong with this.
CharStr := IntTostr('$'+HexStringRep); ?
The argument must be a number, but actually is a string.
DoDi
Right, Sorry, I got my function backwards.
should be the StrToInt.

You see, I know this. Some where in my reply of editing I
must of drifted off!

I am joining the ranks of the others I guess :)

Jamie
Mikey
2012-01-17 15:25:27 UTC
Permalink
Post by Jamie
Post by Hans-Peter Diettrich
Post by Jamie
What is wrong with this.
CharStr := IntTostr('$'+HexStringRep); ?
The argument must be a number, but actually is a string.
DoDi
Right, Sorry, I got my function backwards.
should be the StrToInt.
You see, I know this. Some where in my reply of editing I
must of drifted off!
I am joining the ranks of the others I guess :)
Jamie
Welcome. Page 64 of the handbook gives the details of the secret handshake. ;+}>
S.G
2012-01-18 04:45:19 UTC
Permalink
Post by Mikey
Welcome. Page 64 of the handbook gives the details of the secret handshake. ;+}>
Yes but that is for new comers only. The entry level version of hand
shake to make greenhorns feel they are welcomed.

To find the original, true Frank Borland handshake you have to
-turn Debugger Libraries on,
-turn Full Exceptions on,
-start Delphi IDE in Crash Proof mode
-and then make your code to raise ZERO_DIVIDE error

Only then you will be shown how to make the true, Delphi inner circle
Frank Borland salute.

Very handy, if you are spying in some Visual Basic Meeting or maybe Java
Applets Specialists dinner.

There the Delphi folks can always easily say Hello to each others over
the crowd. And show quick, slanting grin. "These damn ignorant misguided
lost souls...".
S.G.
bgm
2012-09-26 05:54:56 UTC
Permalink
On Fri, 13 Jan 2012 16:51:31 -0800 (PST), battles
Post by battles
I am converting a string of incoming characters that represent a hex
var CharStr: string;
const HexStringRep : string = '616263';
CharStr := char(strtoint('$' + HexStringRep[1] + HexStringRep[2])) +
char(strtoint('$' + HexStringRep[3] +
HexStringRep[4])) +
char(strtoint('$' + HexStringRep[5] +
HexStringRep[6]))...
This works ok, but I was wondering if there is a fancier way of doing
this?
unit Main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, ExtCtrls, StdCtrls, StrUtils;

type
TfrmMain = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
Splitter1: TSplitter;
Panel1: TPanel;
btnHexToString: TButton;
btnStringToHex: TButton;
btnClearAll: TButton;
procedure btnHexToStringClick(Sender: TObject);
procedure btnClearAllClick(Sender: TObject);
procedure btnStringToHexClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function HexToString(Hexy: string): string;
function StripJunk(Junk: string): string;
function StringToHex(Stringy: string): string;
end;

var
frmMain: TfrmMain;

implementation

{$R *.dfm}

//--------------------------------------------------
function TfrmMain.HexToString(Hexy: string): string;
var
i: Integer;
begin
Result:= '';
for i := 1 to length(Hexy) div 2 do
Result:= Result + Char(StrToInt('$' + Copy(Hexy,(I-1)*2+1,2)));
end;
//------------------------------------------------
function TfrmMain.StripJunk(Junk: string): string;
var
JunkSpace, JunkComma: string;
begin
JunkSpace := AnsiReplaceStr(Junk,' ','');
JunkComma := AnsiReplaceStr(JunkSpace,',','');
Result := HexToString(JunkComma);
Memo2.Lines.Append(Result);
end;
//-----------------------------------------------------
function TfrmMain.StringToHex(Stringy: string): string;
var
i, i2: Integer;
s: string;
begin
i2 := 1;
for i := 1 to Length(Stringy) do
begin
Inc(i2);
if i2 = 2 then
begin
s := s + ' ';
i2 := 1;
end;
s := s + IntToHex(Ord(Stringy[i]), 2);
end;
Result := s;
end;
//------------------------------------------------------
procedure TfrmMain.btnHexToStringClick(Sender: TObject);
var
x: integer;
begin
for x := 0 to Memo1.Lines.Count -1 do
begin
StripJunk(Memo1.Lines[x]);
end;
end;
//------------------------------------------------
procedure TfrmMain.btnClearAllClick(Sender: TObject);
begin
Memo1.Clear;
Memo2.Clear;
end;
//------------------------------------------------------
procedure TfrmMain.btnStringToHexClick(Sender: TObject);
var
x: integer;
begin
for x := 0 to Memo1.Lines.Count -1 do
Memo2.Lines.Append(StringToHex(Memo1.Lines[x]));
end;

end.

Loading...