求数据排序方法?

蜀天下100000 2018-05-18 03:51:26
求算法,三个十六进制
报警 正常 待机
00 03 00
03 00 00
3F 00 00
每个节字为一个状态,每个字节换成8位,可表示1-8个端口不定,从低位开始,
00 03 00
0 11 0 表示1、2正常

03 00 00
11 0 0 表示1、2报警


0A 05 00
1010 101 00 表示1、3正常,2、4报警

三个数据,我想把它整合在一起,1号端口在哪个状态下,2号在哪个状态、三号在哪个状态下
有什么好办法排列出来?
...全文
1095 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
用一个word型存储即可,每两位表示一个端口的状态
SupermanTm 2018-05-19
  • 打赏
  • 举报
回复
type
  TPortStatus = (psxAlarm, psxNormal, psxStandby, psxError);
  TPortStatusAry = array [0..7] of TPortStatus;
  TInputData = array [0..2] of Byte;

procedure DecodePortStatus(const Inputs: TInputData; var Outputs: TPortStatusAry);
var
  I: Integer;
  xMask: Byte;
begin
  xMask:= $1;
  for I:= 0 to 7 do begin
    if (Inputs[0] and xMask) <> 0 then
      Outputs[I]:= psxAlarm
    else if (Inputs[1] and xMask) <> 0 then
      Outputs[I]:= psxNormal
    else if (Inputs[2] and xMask) <> 0 then
      Outputs[I]:= psxStandby
    else Outputs[I]:= psxError;
    xMask:= xMask shl 1;
  end;
end;
SupermanTm 2018-05-19
  • 打赏
  • 举报
回复
不困难,但要理清一个问题,冲突(同一个“端口”同时包含在两种状态中),这样算什么状态?
蜀天下100000 2018-05-19
  • 打赏
  • 举报
回复
引用 2 楼 SupermanTm 的回复:
type
  TPortStatus = (psxAlarm, psxNormal, psxStandby, psxError);
  TPortStatusAry = array [0..7] of TPortStatus;
  TInputData = array [0..2] of Byte;

procedure DecodePortStatus(const Inputs: TInputData; var Outputs: TPortStatusAry);
var
  I: Integer;
  xMask: Byte;
begin
  xMask:= $1;
  for I:= 0 to 7 do begin
    if (Inputs[0] and xMask) <> 0 then
      Outputs[I]:= psxAlarm
    else if (Inputs[1] and xMask) <> 0 then
      Outputs[I]:= psxNormal
    else if (Inputs[2] and xMask) <> 0 then
      Outputs[I]:= psxStandby
    else Outputs[I]:= psxError;
    xMask:= xMask shl 1;
  end;
end;
谢谢,我的办法:全部转总数长度的二进制,再从最后一位向前查询1是不是正在?

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧