在德尔福比较阵列 [英] Comparing arrays in Delphi

查看:92
本文介绍了在德尔福比较阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个阵列,例如:

 常量
  答:数组[0..9]字节的=($ 00 $ 01 $ AA,$ A1,$ BB,$ B1,B2 $,$ B3,B4 $,$ FF);
  乙:数组[0..2]字节的=($ A1,$ BB,$ B1);
  C:数组[0..2]字节的=($ 00 $ BB,$ FF);

有没有办法进行比较,并得​​到正确的索引,而不是检查1每个字节1?例如:

 函数GetArrayIndex(来源,值:字节数组):整数;
开始
..
结束;GetArrayIndex(A,B); //结果3
GetArrayIndex(A,C); //结果-1

感谢您提前。


解决方案

 函数ByteArrayPos(常量SearchArr:字节数组;常量CompArr:字节数组):整数;
//结果=位置,或-1如果未找到
VAR
  比较,搜索:AnsiString类型;
开始
  的SetString(小样,PAnsiChar(@CompArr [0]),长度(CompArr));
  的SetString(搜索,PAnsiChar(@SearchArr [0]),长度(SearchArr));
  结果:=波什(搜索,比较) - 1;
结束;

I have 3 arrays, for example:

const
  A: Array[0..9] of Byte = ($00, $01, $AA, $A1, $BB, $B1, $B2, $B3, $B4, $FF);
  B: Array[0..2] of Byte = ($A1, $BB, $B1);
  C: Array[0..2] of Byte = ($00, $BB, $FF);

Is there a way to compare and get the index of the right one, instead of checking each byte 1 by 1? For example:

function GetArrayIndex(Source, Value: Array of Byte): Integer;
begin
..
end;

GetArrayIndex(A, B); // results 3
GetArrayIndex(A, C); // results -1

Thank you in advance.

解决方案

function ByteArrayPos(const SearchArr : array of byte; const CompArr : array of byte) : integer;
//  result=Position or -1 if not found
var
  Comp,Search : AnsiString;
begin
  SetString(Comp, PAnsiChar(@CompArr[0]), Length(CompArr));
  SetString(Search, PAnsiChar(@SearchArr[0]), Length(SearchArr));
  Result := Pos(Search,Comp) - 1;
end;

这篇关于在德尔福比较阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆