[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Converting HEX  strings to ASCII strings

Posted on 2000-04-13
13
Medium Priority
?
359 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 400 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month19 days, 22 hours left to enroll

873 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