//十进制转格雷码
function IntToGray(aInt: longint; codelen: Byte): String;
//十进制转二进制
function IntToBin(Value: LongInt; Size: Integer): String;
var
i: Integer;
begin
Result := '';
for i := Size - 1 downto 0 do
begin
if Value and (1 shl i) <> 0 then
Result := Result + '1'
else
Result := Result + '0';
end;
end;
//二进制转格雷码
function DecimalToGray(Decimal: String): String;
Var
I: Integer;
tmGray, tm: String;
m1, m2, m: Integer;
begin
tm := Decimal;
tmGray := LeftStr(Decimal, 1);
for I := 1 to Length(Decimal) - 1 do
begin
m1 := StrToInt(MidStr(tm, 1, 1));
m2 := StrToInt(MidStr(tm, 2, 1));
m := m1 xor m2;
tmGray := tmGray + IntToStr(m);
tm := MidStr(tm, 2, Length(tm) - 1);
end;
Result := tmGray;
end;
begin
Result := DecimalToGray(IntToBin(aInt, codelen));
end;
delphi十进制转格雷码
最新推荐文章于 2021-05-19 12:05:15 发布