Forum > General

Problems with reads hexadecimal values and conversions

(1/2) > >>

superc:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TMainFormHex.readLoc(startLocation: String;                              byteToRead: Integer;                              Endianness: Integer;                              var oResList: TByteList;                              var oStrErr: string): Boolean;{-------------------------------------------------------------------}function addToBL(var bl: TByteList; byteToAdd: byte): Boolean;begin  Result := false;  try    SetLength(bl, Length(bl) +1);    bl[Length(bl)-1] := byteToAdd;    Result := true;  except  end;end; {-------------------------------------------------------------------} var  F   : THexEditorFrame;  cnt,  n   : Integer;  tres: Integer;  strErr: string;begin  Result := False;  try    F := GetActiveHexEditorFrame;    if Assigned(F) then    begin      if (F.HexEditor = nil) or (not F.HexEditor.HasFile) then        exit;       try        strErr:= '';         n := StrToInt(startLocation);         F.JumpToPosition(n, strErr);         if Length(strErr) > 0 then        begin          oStrErr:= strErr;          exit;        end;         for cnt := 0 to byteToRead -1 do        begin          tres := F.HexEditor.CurrentValue;          addToBL(oResList, tres);          inc(n,1);          F.JumpToPosition(n, strErr);           if Length(strErr) > 0 then          begin            oStrErr:= strErr;            exit;          end;        end;         if Endianness=0 then          THexBitUtility.reverseByteList(oResList);         Result := True;       except        on e: Exception do        begin          oStrErr:=e.Message;        end;      end;    end;  finally   end;end;  class function THexBitUtility.reverseByteList(var byteLst: TByteList;                                              lenghtCouple: Integer): Boolean;var  tempList : TByteList;  bl,  cntStart,  cnt      : Integer;begin  Result := false;  try    tempList := TByteList.Create;    SetLength(tempList, Length(byteLst) );    bl := Length(byteLst);    cntStart:= 0;     for cnt := bl-1 downto 0 do    begin      tempList[cntStart] := byteLst[cnt];      Inc(cntStart);    end;     byteLst := tempList;     Result := true;   finally   end;  Result := true;end;    

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.


jamie:
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.

superc:
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.




jamie:
Use DWORD type for 4 byte unsigned . Same code.

y.ivanov:

--- Quote from: superc on August 09, 2022, 08:50:54 am ---*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.

--- End quote ---
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.

Navigation

[0] Message Index

[#] Next page

Go to full version