Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.pascal.delphi.misc > #167 > unrolled thread

Converting a hex string to its actual character

Started bybattles <battles@mailinator.com>
First post2012-01-13 16:51 -0800
Last post2012-09-26 00:54 -0500
Articles 9 — 7 participants

Back to article view | Back to comp.lang.pascal.delphi.misc


Contents

  Converting a hex string to its actual character battles <battles@mailinator.com> - 2012-01-13 16:51 -0800
    Re: Converting a hex string to its actual character "Heinrich Wolf" <invalid@invalid.invalid> - 2012-01-14 13:53 +0100
      Re: Converting a hex string to its actual character Hans-Peter Diettrich <DrDiettrich1@aol.com> - 2012-01-14 17:53 +0100
    Re: Converting a hex string to its actual character Jamie <jamie_ka1lpa_not_valid_after_ka1lpa_@charter.net> - 2012-01-15 13:55 -0500
      Re: Converting a hex string to its actual character Hans-Peter Diettrich <DrDiettrich1@aol.com> - 2012-01-16 07:42 +0100
        Re: Converting a hex string to its actual character Jamie <jamie_ka1lpa_not_valid_after_ka1lpa_@charter.net> - 2012-01-16 07:39 -0500
          Re: Converting a hex string to its actual character Mikey <nf4l@pobox.com> - 2012-01-17 10:25 -0500
            Re: Converting a hex string to its actual character "S.G" <S.G@none.special.ch> - 2012-01-18 06:45 +0200
    Re: Converting a hex string to its actual character bgm <bgm@frontier.com> - 2012-09-26 00:54 -0500

#167 — Converting a hex string to its actual character

Frombattles <battles@mailinator.com>
Date2012-01-13 16:51 -0800
SubjectConverting a hex string to its actual character
Message-ID<2a914a41-727e-4e78-a03b-65cf348e2259@24g2000yqi.googlegroups.com>
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?

[toc] | [next] | [standalone]


#168

From"Heinrich Wolf" <invalid@invalid.invalid>
Date2012-01-14 13:53 +0100
Message-ID<jertsk$nte$1@news.m-online.net>
In reply to#167
"battles" <battles@mailinator.com> schrieb im Newsbeitrag 
news:2a914a41-727e-4e78-a03b-65cf348e2259@24g2000yqi.googlegroups.com...
>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?

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 

[toc] | [prev] | [next] | [standalone]


#169

FromHans-Peter Diettrich <DrDiettrich1@aol.com>
Date2012-01-14 17:53 +0100
Message-ID<9ndu60F7v6U1@mid.individual.net>
In reply to#168
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

[toc] | [prev] | [next] | [standalone]


#171

FromJamie <jamie_ka1lpa_not_valid_after_ka1lpa_@charter.net>
Date2012-01-15 13:55 -0500
Message-ID<efFQq.356$uf.226@newsfe11.iad>
In reply to#167
battles wrote:
> 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?
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

[toc] | [prev] | [next] | [standalone]


#172

FromHans-Peter Diettrich <DrDiettrich1@aol.com>
Date2012-01-16 07:42 +0100
Message-ID<9nhvkmF5imU1@mid.individual.net>
In reply to#171
Jamie schrieb:

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

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

DoDi

[toc] | [prev] | [next] | [standalone]


#173

FromJamie <jamie_ka1lpa_not_valid_after_ka1lpa_@charter.net>
Date2012-01-16 07:39 -0500
Message-ID<9QUQq.1322$FH7.398@newsfe09.iad>
In reply to#172
Hans-Peter Diettrich wrote:
> Jamie schrieb:
> 
>>   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

[toc] | [prev] | [next] | [standalone]


#174

FromMikey <nf4l@pobox.com>
Date2012-01-17 10:25 -0500
Message-ID<jf43t7$3c6$1@dont-email.me>
In reply to#173
On 1/16/2012 7:39 AM, Jamie wrote:
> Hans-Peter Diettrich wrote:
>> Jamie schrieb:
>>
>>> 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.   ;+}>

[toc] | [prev] | [next] | [standalone]


#175

From"S.G" <S.G@none.special.ch>
Date2012-01-18 06:45 +0200
Message-ID<jf5ip3$df3$1@speranza.aioe.org>
In reply to#174
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.

[toc] | [prev] | [next] | [standalone]


#399

Frombgm <bgm@frontier.com>
Date2012-09-26 00:54 -0500
Message-ID<ht55685o4sntvcld69skdar6l5n554i45f@4ax.com>
In reply to#167
On Fri, 13 Jan 2012 16:51:31 -0800 (PST), battles
<battles@mailinator.com> wrote:

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

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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.pascal.delphi.misc


csiph-web