pascal - 如何将整数转换为字节数组?

标签 pascal

我有一种 ST 代码中的 Action 监听器(类似于 Pascal),它返回一个整数。然后我有一个 CANopen 函数,它允许我仅以字节数组形式发送数据。我如何从这些类型进行转换?

感谢您的回答。

最佳答案

您可以使用Move标准函数将整数 block 复制到四个字节的数组中:

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:我已经几个月没有用 Pascal 编写代码了,所以可能会有错误,请随时修复。

关于pascal - 如何将整数转换为字节数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15038838/

相关文章:

delphi - 创建指向类的新指针

multithreading - 如何从另一个线程中断 ReadLn

Pascal(免费或快速)阅读

compiler-errors - 我的 Pascal 程序警告我有关无法访问的代码

linux - 在 TStringGrid 的单元格中的按钮上绘制和图像

pascal - 如何修复退出代码 201?

installation - Inno Setup if 和语言

Delphi(2006) 循环帮助

dynamic - Inno Setup - 根据系统语言设置默认组件

delphi - boolean 计算错误 : Incompatible types