Recent

Author Topic: [SOLVED] how to convert HexStr result to a simple string?  (Read 3661 times)

beampower

  • Newbie
  • Posts: 6
[SOLVED] how to convert HexStr result to a simple string?
« on: July 14, 2017, 09:45:04 pm »
I'm using BlockRead to read a raw binary data file.
The first 10 bytes of my binary data is a header.
I am able to print off the entire header as 20 characters. Looks great on the screen!
But when I try to extract a sub-string of that header, I run into trouble.
The error I get says Got "Byte", expected "Pointer".
Can I convert my Byte to a Pointer?
I'd like to convert the header to a string representation of the hex and manipulate it as a string.
Code listing is below.

Code: Pascal  [Select][+][-]
  1. program readheader;
  2.  
  3. var
  4.    header : array[1..10] of byte;
  5.    i : integer;
  6.    filename : file of byte;
  7.    numread : word;
  8.    a : string;
  9.  
  10. begin
  11.    assign (filename, 'data.bin');
  12.    reset (filename) ;
  13.  
  14.    // read block (10 bytes)
  15.    blockread (filename, header, sizeof(header), numread);
  16.    writeln;
  17.    write('header = ');
  18.    for i := 1 to 10 do
  19.       write(hexstr(header[i], 2));
  20.  
  21.    // try to get one byte from the header
  22.    a := hexstr(header[1]);
  23.    writeln('substring = ', a);
  24.  
  25.    // or try to get several bytes from the header
  26.    a := hexstr(header[1..5]);
  27.    writeln('substring = ', a);
  28.  
  29.    close (filename);
  30. end.
  31.  


« Last Edit: July 15, 2017, 12:25:57 am by beampower »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: how to convert HexStr result to a simple string?
« Reply #1 on: July 14, 2017, 10:54:54 pm »
The first encountered error on the line that reads: "a := hexstr(header[1]);" is because HexStr function has an overloaded function that accepts a single parameter.

The drawback of that, in such case the provided parameter must be a pointer. See also documentation

Assuming that you wish to have a single character from your header array 'copied' into your  variable named a, then you could use

a := Char(header[1]);

Because your header variable is declared an array of bytes, each individual item inside that array is of type Byte.

So, header[1] will 'extract' the first byte from that array. But, it is still a byte. That is why the compiler would 'stumble' on that as well. Therefor we add an additional cast ( Char(...) ) so that the byte is seen by the compiler as a character.


The second error, most probably stems from the fact that you seem to believe that you can copy a range of characters from an array by providing a range. Pascal is not python and the likes  ;D

So, the line "a := hexstr(header[1..5]);" should look something like "for i := 1 to 5 do a := a + Char(header);"

Here is your complete program with additional 'solutions' to your compilation errors.

Code: Pascal  [Select][+][-]
  1. program readheader;
  2.  
  3. var
  4.    header : array[1..10] of byte;
  5.    i : integer;
  6.    filename : file of byte;
  7.    numread : word;
  8.    a : string;
  9.  
  10. begin
  11.    assign (filename, 'data.bin');
  12.    reset (filename) ;
  13.  
  14.    // read block (10 bytes)
  15.    blockread (filename, header, sizeof(header), numread);
  16.    writeln;
  17.    write('header = ');
  18.    for i := 1 to 10 do
  19.       write(hexstr(header[i], 2));
  20.    WriteLn;
  21.  
  22.    // try to get one byte from the header
  23.    a := hexstr(header[1],2);
  24.    writeln('substring as hex = ', a);
  25.  
  26.    a := Char(header[1]);
  27.    writeln('substring as ascii = ', a);
  28.  
  29.    // or try to get several bytes from the header
  30.    a := '';
  31.    for i := 1 to 5 do a := a + hexstr(header[i],2);
  32.    writeln('substring as hex = ', a);
  33.  
  34.    a := '';
  35.    for i := 1 to 5 do a := a + Char(header[i]);
  36.    writeln('substring as ascii = ', a);
  37.    
  38.    SetString(a, PChar(@header[1]), 4);
  39.    writeln('substring as ascii = ', a);
  40.  
  41.    close (filename);
  42. end.
  43.  

There are other solutions possible as well, but hopefully the above would be enough to get you going.
« Last Edit: July 14, 2017, 11:19:41 pm by molly »

beampower

  • Newbie
  • Posts: 6
Re: how to convert HexStr result to a simple string?
« Reply #2 on: July 14, 2017, 11:44:37 pm »
Success!
Thanks Molly, you are indeed a hero.
I can finally get back to finishing my code.

results:
header = 3EA9DE289DC8A3558142
substring as hex = 3E
substring as ascii = >
substring as hex = 3EA9DE289D
substring as ascii = >¬¦(¥
substring as ascii = >¬¦(

Yes, I am stuck in a Python programming mode.

By the way, is it okay to use variables as the start and stop points of a Do Loop?
I'm working on a function to extract a sub-string from say byte 3 to byte 9.

Now if I can only figure out why I keep getting Signal 291 errors on my Free Pascal  Compiler (FPC)?

Thanks again.

beampower

  • Newbie
  • Posts: 6
Re: how to convert HexStr result to a simple string?
« Reply #3 on: July 14, 2017, 11:50:33 pm »
How do you declare this as solved?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: how to convert HexStr result to a simple string?
« Reply #4 on: July 15, 2017, 12:04:28 am »
By the way, is it okay to use variables as the start and stop points of a Do Loop?
Yes, that is fine, but remember that the iterator itself is not allowed to be modified inside the loop unless it's a while do or repeat until loop.

Quote
I'm working on a function to extract a sub-string from say byte 3 to byte 9.
Perhaps it's is allowed for you to throw in some OOP ? Using a TFileStream would be so much easier for your task, e.g.

Code: Pascal  [Select][+][-]
  1. var
  2.   FS: TFileStream;
  3.   B: Byte;
  4.   W: Word;
  5.   DW: DWord;
  6. begin
  7.   FS := TFileStream.Create('filename', fmOpenread);
  8.   try
  9.    FS.Position := 0;
  10.    While FS.Position < FS.Size do
  11.    begin
  12.     B := FS.ReadByte;
  13.     W := FS.ReadWord;
  14.     DW := FS.ReadDWord
  15. //   etc. etc. more methods available, follow the links (ancestor classes) inside documentation
  16. //  such as ReadBuffer, where you can define the number of bytes and read them into an array
  17. //  directly.
  18.    end;
  19.   finally
  20.    FS.Free
  21.   end;
  22. end;
  23.  

Quote
Now if I can only figure out why I keep getting Signal 291 errors on my Free Pascal  Compiler (FPC)?
Sorry, no idea. But that doesn't sound too good  :-\

How do you declare this as solved?
Login, edit your first post that started this thread, and write [SOLVED] in front of your topic header, the post the message again.

 

TinyPortal © 2005-2018