Recent

Author Topic: Problems with reads hexadecimal values and conversions  (Read 864 times)

superc

  • Full Member
  • ***
  • Posts: 241
Problems with reads hexadecimal values and conversions
« on: August 03, 2022, 04:24:36 pm »
Hello,

i'm going crazy for a problem i can't solve...

as in the image I have to extract the hexadecimal values ​​represented as '00:08' in Little Endian and then turn them over: the final result must be '0x0800' that is 2048 decimal.

Well, for read multiple location I writed a function like this

Code: Pascal  [Select][+][-]
  1. function TMainFormHex.readLoc(startLocation: String;
  2.                               byteToRead: Integer;
  3.                               Endianness: Integer;
  4.                               var oResList: TByteList;
  5.                               var oStrErr: string): Boolean;
  6. {-------------------------------------------------------------------}
  7. function addToBL(var bl: TByteList; byteToAdd: byte): Boolean;
  8. begin
  9.   Result := false;
  10.   try
  11.     SetLength(bl, Length(bl) +1);
  12.     bl[Length(bl)-1] := byteToAdd;
  13.     Result := true;
  14.   except
  15.   end;
  16. end;
  17.  
  18. {-------------------------------------------------------------------}
  19.  
  20. var
  21.   F   : THexEditorFrame;
  22.   cnt,
  23.   n   : Integer;
  24.   tres: Integer;
  25.   strErr: string;
  26. begin
  27.   Result := False;
  28.   try
  29.     F := GetActiveHexEditorFrame;
  30.     if Assigned(F) then
  31.     begin
  32.       if (F.HexEditor = nil) or (not F.HexEditor.HasFile) then
  33.         exit;
  34.  
  35.       try
  36.         strErr:= '';
  37.  
  38.         n := StrToInt(startLocation);
  39.  
  40.         F.JumpToPosition(n, strErr);
  41.  
  42.         if Length(strErr) > 0 then
  43.         begin
  44.           oStrErr:= strErr;
  45.           exit;
  46.         end;
  47.  
  48.         for cnt := 0 to byteToRead -1 do
  49.         begin
  50.           tres := F.HexEditor.CurrentValue;
  51.           addToBL(oResList, tres);
  52.           inc(n,1);
  53.           F.JumpToPosition(n, strErr);
  54.  
  55.           if Length(strErr) > 0 then
  56.           begin
  57.             oStrErr:= strErr;
  58.             exit;
  59.           end;
  60.         end;
  61.  
  62.         if Endianness=0 then
  63.           THexBitUtility.reverseByteList(oResList);
  64.  
  65.         Result := True;
  66.  
  67.       except
  68.         on e: Exception do
  69.         begin
  70.           oStrErr:=e.Message;
  71.         end;
  72.       end;
  73.     end;
  74.   finally
  75.  
  76.   end;
  77. end;
  78.  
  79. class function THexBitUtility.reverseByteList(var byteLst: TByteList;
  80.                                               lenghtCouple: Integer): Boolean;
  81. var
  82.   tempList : TByteList;
  83.   bl,
  84.   cntStart,
  85.   cnt      : Integer;
  86. begin
  87.   Result := false;
  88.   try
  89.     tempList := TByteList.Create;
  90.     SetLength(tempList, Length(byteLst) );
  91.     bl := Length(byteLst);
  92.     cntStart:= 0;
  93.  
  94.     for cnt := bl-1 downto 0 do
  95.     begin
  96.       tempList[cntStart] := byteLst[cnt];
  97.       Inc(cntStart);
  98.     end;
  99.  
  100.     byteLst := tempList;
  101.  
  102.     Result := true;
  103.  
  104.   finally
  105.  
  106.   end;
  107.   Result := true;
  108. end;  
  109.  


With 'F.HexEditor.CurrentValue' I obtain a decimal value from 0 to 254: in the end I basically get 8 as a value and not 800.

I can't find another way to fix this,
thank you in advance.


« Last Edit: August 03, 2022, 04:50:02 pm by superc »

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Problems with reads hexadecimal values and conversions
« Reply #1 on: August 03, 2022, 11:13:58 pm »
if you are reading it in byte segments then do that in 2 steps.

first byte you read you do this.

FirstByte := FirstBtye shl 8;

make sure the firstbyte variable is a word or larger.

then the second step.

 FirstByte := FirstByte Or SecondByte.

now you have your two bytes in reverse order.

or you could read it as a word and use the SWAP or SWAPEndian etc.
The only true wisdom is knowing you know nothing

superc

  • Full Member
  • ***
  • Posts: 241
Re: Problems with reads hexadecimal values and conversions
« Reply #2 on: August 09, 2022, 08:50:54 am »
Thank you very much jamie, just what i need, but at this point i have more questions:

  • for read 4 byte I used a uint16 (smallint) or word, but  using negative datatypes numbers is useless?
  • for calculate a negative number, Lazarus have function for this? (complement 1, and complement2)

Thank in advance.

UPDATE:
I found bitwise operator usage at this link
https://www.tutorialspoint.com/pascal/pascal_bit_operators.htm

but tilde operator doesn't works, I receive an error likes 'Fatal: illegal character "'~'" ($7E)', why???

Thanks in advance.




« Last Edit: August 09, 2022, 12:12:53 pm by superc »

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Problems with reads hexadecimal values and conversions
« Reply #3 on: August 09, 2022, 01:01:31 pm »
Use DWORD type for 4 byte unsigned . Same code.
The only true wisdom is knowing you know nothing

alpine

  • Hero Member
  • *****
  • Posts: 1038
Re: Problems with reads hexadecimal values and conversions
« Reply #4 on: August 09, 2022, 02:03:11 pm »
*snip*

UPDATE:
I found bitwise operator usage at this link
https://www.tutorialspoint.com/pascal/pascal_bit_operators.htm

but tilde operator doesn't works, I receive an error likes 'Fatal: illegal character "'~'" ($7E)', why???

Thanks in advance.
Strange... AFAIK tilde has never been operator in the Pascal language. Into the same tutorial there are boolean operators and then, or else which aren't either.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

superc

  • Full Member
  • ***
  • Posts: 241
Re: Problems with reads hexadecimal values and conversions
« Reply #5 on: August 09, 2022, 02:20:14 pm »
Now I wrote this code
Code: Pascal  [Select][+][-]
  1. var ub: int16;
  2. begin
  3.       ub := 0;
  4.       ub := Hex2Dec('EC');
  5.       ub := ub shl 8;
  6.       ub := ub or Hex2Dec('FF');
  7.       ub := SwapEndian(ub);
  8.       ShowMessage(IntToStr(ub));
  9. end;
  10.  

I obtain -20, and this for me correct;

If i substitute int16 with uint16 or with word I obtain 65516,  If i substitute int16 with Integer I obtain -1310720:

so I have to know in advance if the value I am going to read could be negative or not and its maximum dimension.

Thanks in advance.

 

TinyPortal © 2005-2018