Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: Buffer Edit Box

  1. #1
    Join Date
    Jul 2007
    Posts
    42

    Buffer Edit Box

    Hi, I'm trying to make a small program to edit 2 bytes of data of a bin file.
    I'm loading the whole bin file into the buffer and the offsets I want to edit are at 6E088 - 6E089

    Code:
    6E080 00 00 00 00 00 00 00 00 05 00 0B 00 0B 02 17 02 
    6E090 17 22 17 62 18 00 18 01 01 00 06 02 06 04 06 28 
    6E0A0 0D 00 0D 01 0D 03 0D 04 0D 05 0D 22 26 00 4A D1 
    6E0B0 09 19 00 00 00 00 00 00 00 00 00 00 00 00 00 00

    This code below loads the file into the buffer then puts the data at 6E088 - 6E089 into an edit1.text box. However in the edit box it doesn't display 05 00, it displays garbage. I need the data to display in ascii but I don't have a clue how to it. Any suggestion code would be most welcome guys. Thanks

    Code:
    var
      Form1: TForm1;
      Hex : string;
      F    : file;
      counter : integer;
      Buffer : array [0..745964] of char;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.Button2Click(Sender: TObject);
    begin
    if OpenDialogHex.Execute = false then exit;
    Hex := OpenDialogHex.FileName;
    AssignFile(F, Hex) ;
    Reset(F,1);
    counter:= sizeof(buffer);
    While counter = sizeof(buffer) do
    begin
    try
      FillChar(buffer, sizeof(buffer), #0);
      BlockRead(F, Buffer,sizeof(buffer),counter);
    finally
      edit1.Text := (buffer[450696] + buffer[450697] + buffer[450698] + buffer[450699]) ;
      end;
    End;
    CloseFile(F) ;
    end;

  2. #2
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    Thats because its giving you a character representation of the data, you need to convert the values to hex, and then to string.. and to save them into the buffer, you would need to turn froms tring to hex, to byte.
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

  3. #3
    Join Date
    Jul 2007
    Posts
    42
    Hi Liz

    Thats where I'm stuck I don't know how to convert the binary data values to hex and then a string.

    Please could you show with some example code?

  4. #4
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    have a look at inttohex
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

  5. #5
    Join Date
    Jul 2007
    Posts
    42
    I've tried the following but I get this error when I try to compile
    [Pascal Error] giga.pas(77): E2250 There is no overloaded version of 'IntToHex' that can be called with these arguments

    Code:
    var
      Hex : String;
      F   : File;
      aString : string;
      aInt : integer;
      bInt : integer;
      counter: integer;
      Buffer : Array [0..800000] of char;
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      if OpenDialogHex.Execute then begin
    Hex := OpenDialogHex.FileName;
    AssignFile(F, Hex) ;
    Reset(F,1);
    counter:= sizeof(buffer);
    While counter = sizeof(buffer) do
    begin
    try
      FillChar(buffer, sizeof(buffer), #0);
      BlockRead(F, Buffer,sizeof(buffer),counter);
    finally
      aInt := strtoint (buffer[$6E088]);
      bInt := inttohex (aInt);   <--------------------- [[Pascal Error] giga.pas(77): E2250 There is no overloaded version of 'IntToHex' that can be called with these arguments]
    
      Edit1.Text := (bInt) ; 
    end;
    End;
    CloseFile(F) ;
      end;
    end;

  6. #6
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    Take a closer look at what inttohex actually does..

    Its right.. read again what inttohex outputs
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

  7. #7
    Join Date
    Jul 2007
    Posts
    42
    I'm getting nowwhere fast Liz.

    I'm peeing in the wind

  8. #8
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    well, let me hand you a dunce hat and point out that the helpfile clearly states inttohex outputs a STRING not an integer.
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

  9. #9
    Join Date
    Jul 2007
    Posts
    42
    I'll have you know my dunce hat is a permanent fixture and fashionable head accessory

    Now my hats on can you help me solve this little problem.

    The offset addresses $6E088 and $6E089 are stored in aString and the function does the string to hex conversion. The problem is if the value in the offset address is 00 which it is in offset $6E089 then nothing is displayed in the edit1.text box

    Here's where I'm at now...

    Code:
    6E080 00 00 00 00 00 00 00 00 05 00 0B 00 0B 02 17 02 
    6E090 17 22 17 62 18 00 18 01 01 00 06 02 06 04 06 28 
    6E0A0 0D 00 0D 01 0D 03 0D 04 0D 05 0D 22 26 00 4A D1 
    6E0B0 09 19 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    Code:
    var
      Hex : string;
      F    : file;
      counter : integer;
      Stringa : string;
      aString : string;
      Buffer : array [0..800000] of char;
    
    function StringtoHex(Stringa: string): string;
    var
      a :   Integer;
      Conversion: string;
    begin
      for a := 1 to Length(Stringa) do
      begin
        Conversion := Conversion + IntToHex(ord(Stringa[a]), 2);
      end;
      Result := Conversion;
    end;
    
    
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      if OpenDialogHex.Execute then begin
        Hex := OpenDialogHex.Filename;
    AssignFile(F, Hex) ;
    Reset(F,1);
    counter:= sizeof(buffer);
    While counter = sizeof(buffer) do
    begin
    try
      FillChar(buffer, sizeof(buffer), #0);
      BlockRead(F, Buffer,sizeof(buffer),counter);
    finally
    aString := buffer[$6E088] + buffer[$6E089];
    Edit1.Text := (aString);
    Edit1.Text := StringtoHex(Edit1.Text);
    end;
    End;
    CloseFile(F) ;
    end;
    
    end;
    Last edited by lda; 02-28-2008 at 05:45 AM.

  10. #10
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    OK, dont ram the hat on too hard though, it may not come off :P

    A few pointers:

    1. You read in chars - you potentially munge characters that way, read in bytes
    2. your string to hex function - is confusing.. and very unnecessary - chances are this is not producing the output you expected
    3. you read the entire thing, and then only use 2 bytes, why not just read them out.
    4. dont make your string that way :P do astring:=inttoex(buffer(...),2)+' '+inttoex(buffer(...),2); no point going that mad over it all.
    5. are you sure your files read in the way you thought? eg, you have been reading into a buffer in chunks, other than as I said, are you sure the values in the 2 entries are actually there?? did you look in the debugger and see them?

    Personally drop the huge unnecessary array
    read in just the 2 bytes you need
    display them and convert using inttohex
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •