[ Solved ]conversion from int to hex lazarus / 21-General ---------------------------------------------------------------------- superc · 2022-09-02 10:02 Hello, sorry if I go back to topics already covered but I am not clear about some concepts: as in the picture, I have those two values ​​in hexadecimal in little endian: b603 which overturned is 3b6  ( value 950) and now I read the values ​​correctly; my problem now is having to write different values ​​such as 978 that is 3d2 in hex and then reverse it and get 23d: I should therefore write in the first location '02' and in the second '3d'. I have a function that writes an array of bytes starting from one location forward for how many bytes there are in the array: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     tempList := TByteList.Create;     SetLength(tempList,2);     tempList[0] := 2;    tempList[1] := 61;    gotoLocModifyMulti('220644', strErr, tempList, 2 );       now i can't figure out how to solve this step ie i have to convert my number i want to hex, but how do i smear it correctly in a byte array? Sorry again but for me this is a new and difficult topic, Thanks in advance. ---------------------------------------------------------------------- beria · 2022-09-02 10:12 As far as I understand, there are standard functions to reverse the order of bytes (words) or to split them up unit JwaWinType; ---- function MAKEWORD(a, b: BYTE): WORD; function MAKELONG(a, b: WORD): DWORD; function LOWORD(L: DWORD): WORD; function HIWORD(L: DWORD): WORD; function LOBYTE(W: WORD): BYTE; function HIBYTE(W: WORD): BYTE; ---- ---------------------------------------------------------------------- Thaddy · 2022-09-02 10:32 You don't need those. They are similar in system and in sysutils. And jwaWinTypes is windows only. See https://www.freepascal.org/docs-html/rtl/system/hi.html and https://www.freepascal.org/docs-html/rtl/sysutils/typehelpers.html  and https://www.freepascal.org/docs-html/rtl/system/swap.html The type helper ToHexSTring formats, swap can change endianness for high and low values in a word or integer based on lo/hi. All these are cross-platform. e.g:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}uses   sysutils;var   i:word = $03d2;begin  writeln(i.ToHexString);  writeln(swap(i).ToHexString);end.Stuffing a byte array can simply be done by reading words and set the array elements with Lo()/Hi(). That is if you want to change the endianness, otherwise it is Hi()/Lo() ---------------------------------------------------------------------- superc · 2022-09-02 12:16 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   i := 976;  memoDebug.Lines.Add(i.ToHexString);  memoDebug.Lines.Add(swap(i).ToHexString);     You get: 03D0 D003 What I did not understand how to insert in this case 'D0' in the first element of the dynamic array and so on.. Thank in advance. ---------------------------------------------------------------------- Thaddy · 2022-09-02 15:07 One option is:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}uses sysutils;var   i:word = 976;  j:byte; // used as array element, not index!  MyArray:array of byte;begin  SetLength(MyArray,SizeOf(i));  MyArray[0] := lo(i);  MyArray[1] := hi(i);  for j in myarray do writeln(j.tohexstring); // only need ToHexString for display.end. ---------------------------------------------------------------------- KodeZwerg · 2022-09-02 16:10 Maybe my helpers can help you too, but my base is byte not word, thats why many things are easier but i added a conversion also for that purpose  this little toy works same like suggested Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function CreateWord(const LoValue, HiValue: Byte): Word; inline;var  LResult: array[0..1] of Byte absolute Result;begin  LResult[0] := LoValue;  LResult[1] := HiValue;end;  this little toy convert a byte to a hex string Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function ByteToHex(const AValue: Byte): AnsiString;const  CHex: array[0..15] of AnsiChar = '0123456789ABCDEF';begin  Result := CHex[AValue shr 4] + CHex[AValue and $0F];end;  this little toy reverse a Hex back to a Byte Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function HexToByte(const AHexValue: AnsiString): Integer;begin  TryStrToInt('$' + AHexValue, Result);end;  I hope it helps. ---------------------------------------------------------------------- Thaddy · 2022-09-02 19:45 No, not at all. Overly complex. Your remark about word vs byte is also nonsense. ---------------------------------------------------------------------- avra · 2022-09-02 23:45 Quote from: superc on September 02, 2022, 02:16:29 pmWhat I did not understand how to insert in this case 'D0' in the first element of the dynamic array and so on.. Latest trunk allows you this if you include new syshelpers unit (modified Thaddy's nice example): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1;{$mode delphi} uses  syshelpers; var  i: word = 976;  j: byte;  Arr1, Arr2: array[0..1] of byte; begin  WriteLn(i.ToHexString(false)); // 3D0   Arr1[0] := i.Bytes[0];  Arr1[1] := i.Bytes[1];  for j in Arr1 do    Write(j.ToHexString + ' ');  // D0 03  WriteLn;   Arr2[0] := i.Bytes[1];  Arr2[1] := i.Bytes[0];  for j in Arr2 do    Write(j.ToHexString + ' ');  // 03 D0  WriteLn;end. Of course, Bytes[] can be used in a loop, too. More info about these new helpers can be found in this demo: https://gitlab.com/freepascal.org/fpc/source/-/blob/main/packages/rtl- objpas/examples/syshelpersdemo.pas Thinking about original problem, you probably read bytes from a file and then populate some byte array, or even show bytes directly with gui control. All you actually need is to read byte, and if it is odd indexed then write to even byte, and if it is even indexed byte then write it to odd byte (or even simpler do just that on the fly in gui control and save memory on additional byte array). ---------------------------------------------------------------------- KodeZwerg · 2022-09-03 06:03 Quote from: Thaddy on September 02, 2022, 09:45:44 pmNo, not at all.I am sorry, i did really thought that it can help to speed up things. Quote from: Thaddy on September 02, 2022, 09:45:44 pmOverly complex.One-Liners are very complex, you are correct. Quote from: Thaddy on September 02, 2022, 09:45:44 pmYour remark about word vs byte is also nonsense.I did remarked something? So yes. In my opinion when dealing with binary data, i go with bytes, no need to adjust or whatever, byte on offset 1234 is byte on offset 1234, no matter what. Enjoy your thing. ---------------------------------------------------------------------- Thaddy · 2022-09-03 11:01 For OP: you can also include sockets and use the htons function (or htonl for 4 bytes):Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode delphi}uses sysutils, sockets;begin  writeln(htons(950).tohexstring);end. ---------------------------------------------------------------------- BobDog · 2022-09-03 19:19 Word reversing using a byte array from first principles. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  function reverse(i:word):word; typeunion =recordcase integer of0:(a:word);1:(b:array[1..2] of byte);end; procedure switch(var a, b: byte);var temp: byte;begin  temp := a; a := b; b := temp;end; varu:uni on;beginu.a:=i;switch(u.b[1],u.b[2]);exit(u.a);end;  vari,n:int32; beg inrandomize;for n:=1 to 10 dobegini:=random(500);writeln(hexstr(i,4),' = ',i);writeln(hexstr(reverse(i),4));writelnend;writeln('Press return to end . . .');readln; end. ---------------------------------------------------------------------- superc · 2022-09-05 10:39 Thanks, they are all excellent solutions, however I think the most congenial is that of Thaddy: I solved it like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     TTypeOfData.PAR_TYPE_SIGNED,    TTypeOfData.PAR_TYPE_UNSIGNED: begin                                   if not TryStrToInt(value, tempInt) then                                       exit;                                    num1 := tempInt;                                     tempList := TByteList.Create;                                     SetLength(tempList,2);                                      tempList[0] := Hi(num1);                                     tempList[1] := Lo(num1);                                      if (Endianness = 0) then                                       reverseByteList(tempList, strErr);                                    gotoLocModifyMulti(startAddress, strErr, tempList, SIZE_SHORT_INT);                                   end;     depending on the type of data and therefore how the data is written in the bin file I cannot always use the 'SwapEndian'; in these answers there are a lot of topics that I didn't know as well as the helpers, which I will deepen.... ---------------------------------------------------------------------- jamie · 2022-09-05 11:35 is there something wrong with "SwapEndian" function ? ---------------------------------------------------------------------- superc · 2022-09-05 12:11 Quote from: jamie on September 05, 2022, 01:35:47 pmis there something wrong with "SwapEndian" function ? what are you referring to? ---------------------------------------------------------------------- Thaddy · 2022-09-05 12:22 The manual? https://www.freepascal.org/docs-html/rtl/system/swapendian.html Which basically does what I told you before. ---------------------------------------------------------------------- superc · 2022-09-05 12:59 Quote from: Thaddy on September 05, 2022, 02:22:25 pm ahhhh this sigh reminds me a lot  stack overflow .... also the flag I must say, it is a behavior that I recognize from a very nice user of that community ..