?
Solved

Converting HEX  strings to ASCII strings

Posted on 2000-04-13
13
Medium Priority
?
350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

741 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