Solved

Converting HEX  strings to ASCII strings

Posted on 2000-04-13
13
321 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:Ben_iti
  • 4
  • 3
  • 3
  • +3
13 Comments
 
LVL 7

Accepted Solution

by:
Motaz earned 100 total points
ID: 2711614
Use this instead:

AssignFile(F, 'Love.midi');
  Rewrite(F);
  Writeln(F , $4D, $54, $68, $64, $00, $00, $00);
  CloseFile(F); { Close file, save changes }

Motaz
0
 

Author Comment

by:Ben_iti
ID: 2711620
No Comments

Have a bogus day
0
 

Author Comment

by:Ben_iti
ID: 2711662
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
0
 
LVL 7

Expert Comment

by:Motaz
ID: 2711692
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;
0
 
LVL 6

Expert Comment

by:Jaymol
ID: 2711703
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.
0
 
LVL 6

Expert Comment

by:Jaymol
ID: 2711707
Oh yeah, it will actually show MTHD not Mthd  (All uppercase character values.)

John.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 6

Expert Comment

by:Jaymol
ID: 2711712
Ignore that stupid comment.

John.
0
 
LVL 2

Expert Comment

by:mullet_attack
ID: 2711746
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.
0
 
LVL 6

Expert Comment

by:Jaymol
ID: 2711755
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.
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2711809

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

0
 
LVL 2

Expert Comment

by:florisb
ID: 2713063
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.
0
 

Author Comment

by:Ben_iti
ID: 2714929
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


0
 
LVL 7

Expert Comment

by:Motaz
ID: 2718599
Welcome any time Ben Iti, I'm happy that I was helped you.

Motaz
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now