Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert integer to array of bytes?

Tags:

pascal

I have sort of action listener in ST code (similar to Pascal), where it returns me an integer. Then i have a CANopen function, which allows me to send data only in Array of bytes. How can i convert from these types?

Thanks for answer.

like image 831
Silvester Jakša Avatar asked Jan 21 '26 20:01

Silvester Jakša


1 Answers

You can use the Move standard function to block-copy the integer into an array of four bytes:

var
    MyInteger: Integer;
    MyArray: array [0..3] of Byte;
begin
    // Move the integer into the array
    Move(MyInteger, MyArray, 4);

    // This may be subject to endianness, use SwapEndian (and related) as needed

    // To get the integer back from the array
    Move(MyArray, MyInteger, 4);
end;

PS: I haven't coded in Pascal for a few months now so there might be mistakes, feel free to fix.

like image 126
Thomas Avatar answered Jan 25 '26 08:01

Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!