Link to home
Start Free TrialLog in
Avatar of Ben_iti
Ben_iti

asked on

Converting HEX strings to ASCII strings

I need to find out how to convert Hex strings to Ascii Strings

Example

AssignFile(F, 'Love.midi');
  Rewrite(F);
  Writeln(F , ':4D 54 68 64 00 00 00 '  );    // This is the Hex
                                              // String
  CloseFile(F);      { Close file, save changes }

and after when you load the created file 'love.midi' through a txt editor it must be in ascii format and not in hex code (hex ascii code)

The WriteIn(F , ':4D 54 68 00 00 00' );
on a midi file using a text editor to view the written string is written as

4D 54 68 00 00 00

instead of the preferred way that I want which is

MThd...

Please reply

Ben
ASKER CERTIFIED SOLUTION
Avatar of Motaz
Motaz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ben_iti
Ben_iti

ASKER

No Comments

Have a bogus day
Avatar of Ben_iti

ASKER

No im sorry Motaz
That did not work

It came up viewed using a text editor as this

7784104100000

and not like this

Mthd...

Keep trying, for a minute there
I thought you actually had the answer

Thanks
Ben
I didn't understand what did you want exactly.

Any whay you may use Typed file instead:

var
  f: file of byte;
  b: Byte;
begin
  AssignFile(F, 'Love.midi');
  Rewrite(F);
  B:= $4D;
  Write(F, B);
  B:= $54;
  Write(F, B);
  ...
  CloseFile(F);
end;
You need to change one line of Motaz's code to this....

Writeln(F , #$4D, #$54, #$68, #$64, #$00, #$00, #$00);

Motaz still deserves the points though.

John.
Oh yeah, it will actually show MTHD not Mthd  (All uppercase character values.)

John.
Ignore that stupid comment.

John.
Perhaps the best way would be to use
"Format", eg

Format('%2.2x',[$4D])

This will write 2 hex digits, left padded if necessary.

or Format('3.3d',[$4D])

This will write 3 decimal digits, left padded if necessary.

I'm not quite sure which one you want.

You could use a loop and "copy" to parse the string of ascii text and write it your file.
Huh?????

Just use the original code posted by Motaz (the nicest looking) and alter in the way I showed.

It's the easiest and nicest to look at.

Like I said, Motaz got it right, apart from that one little oversight.

John.

header:='YCHT'+chr(0)+chr(0)+chr(0)+chr(1)+chr(0) +chr(0)+chr(0)+chr(0)+chr(1);
  chr(length(username))+chr(length(cookie))+chr(4);
for l:=1 to length(header) do
  begin
  mychar:=header[l];
    buffer[l]:=mychar;
  end;
for l:=length(header) to length(header+username) do
  begin
    buffer[l]:=username[l];
  end;

{

ycht   +    ver +  type +   size +    name  + del1 +  cookie

where,
ycht     = YCHT
ver      = 0010
login    = 0001
size     = len(name)  + len(cookie) + len(del1)
name     = username
cookie   = cookie
del1     = 0x01

}


I'm having a problem kinda the same.... this is whats needed to do, but i bet if this is solved then, his problem is too..... but with this you have a multiple of vars, including size the name.....

header:='YCHT'+chr(0)+chr(0)+chr(0)+chr(1)+chr(0) +chr(0)+chr(0)+chr(0)+chr(1);  <<Is this the way to go?.... for you love.midi thing?.. or is there no easy solution to this....

not what you ask, fast post. 'move' is related to..
Floris.

unit Unit1; interface

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

type TStr25 = string[25];
     TStr4  = string[4];
     TStr9  = string[9];
     Tstr8 = string[8];
     Tstr1 = string[1];

Type
  PEmp1St = ^TEmp1St;
  TEmp1St = Record
                ANaam     : TStr25;  
                Straat    : TStr25;
                Plaats    : TStr25;
  end;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
Var
  a   : SmallInt;
  Mw1 : TEmp1St;
  Mw2 : TEmp1St;
  T : Array[1..2000] Of Byte;
  u,v : widestring;
  w : byte;
Begin
  //init Mw1, Mw2
  FillChar( Mw1, SizeOf( TEmp1St), 0);
  FillChar( Mw2, SizeOf( TEmp1St), 0);

  //values Mw1
  Mw1.ANaam := 'floris';
  Mw1.Straat := 'jaja';
  Mw1.Plaats := 'Adam';

  //Mw1 to array
  Move( Mw1, T, SizeOf( TEmp1St));

  //now convert T to a widestring.
  u := '';
  for a := 1 to 2000 do
    begin
    u := u + char(T[a]);
    end;

  //now convert widestring again to T
  for a := 1 to 2000 do
    begin
    T[a] := byte(u[a]);
    end;

  // move T to Mw2 and show some fields.
  Move( T, Mw2, SizeOf( TEmp1St));
  showMessage(Mw2.Anaam+';'+Mw2.Straat+';'+Mw2.plaats);
end;
end.
Avatar of Ben_iti

ASKER

Thankyou Motaz

You were the first to reply to my question, the funniest thing
you answered my email 30 seconds later
That only tells me that your a pro.

I thankyou for your answer and for your quick reply.

The 100 points go to you.

I would also like to thank Jaymol (John)
who also replied to my email saying for me to add those nifty '#'s to Motaz code
I Thankyou both

Ben Iti


Welcome any time Ben Iti, I'm happy that I was helped you.

Motaz