Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Converting a hex string to its actual character

2,725 views
Skip to first unread message

battles

unread,
Jan 13, 2012, 7:51:31 PM1/13/12
to
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

unread,
Jan 14, 2012, 7:53:36 AM1/14/12
to

"battles" <bat...@mailinator.com> schrieb im Newsbeitrag
news:2a914a41-727e-4e78...@24g2000yqi.googlegroups.com...
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

unread,
Jan 14, 2012, 11:53:30 AM1/14/12
to
Heinrich Wolf schrieb:

> 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.

> 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

unread,
Jan 15, 2012, 1:55:56 PM1/15/12
to
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

unread,
Jan 16, 2012, 1:42:47 AM1/16/12
to
Jamie schrieb:

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

The argument must be a number, but actually is a string.

DoDi

Jamie

unread,
Jan 16, 2012, 7:39:10 AM1/16/12
to
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

unread,
Jan 17, 2012, 10:25:27 AM1/17/12
to
Welcome. Page 64 of the handbook gives the details of the secret handshake. ;+}>

S.G

unread,
Jan 17, 2012, 11:45:19 PM1/17/12
to
Mikey wrote:

> 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

unread,
Sep 26, 2012, 1:54:56 AM9/26/12
to
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.
0 new messages