M
M
Mikhail Petrov2018-12-18 13:45:49
Delphi
Mikhail Petrov, 2018-12-18 13:45:49

How to expand a string?

There is a string "4169BB8C", how to reverse it by 2 characters to get "8CBB6941"?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevDemi, 2018-12-18
@DevDemi

procedure TForm1.Button1Click(Sender: TObject);
var
 st,st1:string;
 i,l:integer;
begin
 st:='4169BB8C';   // => 8CBB6941
 l:=length(st);
 st1:='';
 i:=1;
 repeat
  st1:=st1+st[l-i]+st[l-i+1];
  i:=i+2;
 until i>=l;
 Form1.Caption:=st+'=>'+st1;
end;

K
krka92, 2019-03-25
@krka92

function RevertString(Value: String): String;
var
  PValue, PResult, PStartValue: PChar;
  LenValue: Integer;
begin
  LenValue := Length(Value);
  if LenValue mod 2 > 0 then raise Exception.Create('По 2 символа не получтися');

  PValue := PChar(Value);
  PStartValue := PValue;
  SetLength(Result, LenValue);
  PResult := PChar(Result);
  Inc(PValue, LenValue - 2);

  while PValue >= PStartValue do
  begin
    Move(PValue^, PResult^, 4);
    Inc(PResult, 2);
    Dec(PValue, 2);
  end;
end;

Since concatenation is evil)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question