开发者

How can I strip leading zeros from a hexadecimal string?

If I have a string as '0000FFFF' or '0000F0F0', how can have the output be respectively 'FFFF' an开发者_运维问答d 'F0F0', deleting the non-significant 0 from it?


This function will strip leading zeros:

function StripLeadingZeros(const s: string): string;
var
  i, Len: Integer;
begin
  Len := Length(s);
  for i := 1 to Len do begin
    if s[i]<>'0' then begin
      Result := Copy(s, i, Len);
      exit;
    end;
  end;
  Result := '0';
end;


Format('%X', [StrToInt('$' + number)])


function mystrip(Value: string): string; 
var 
  Flag: Boolean; 
  Index: Integer; 
begin 
  Result := ''; Flag := false; 
  for Index := 1 to Length(Value) do 
  begin 
    if not Flag then 
    begin 
      if (Value[Index] <> #48) then 
      begin 
        Flag := true; 
        Result := Result + Value[Index]; 
      end 
    end 
  else 
    Result := Result + Value[Index]; 
  end; 
end; 


This problem splits into two parts and solves in 4 LoC (or 5 with explaining variable).

licensed under GPL

function TrimLeading(const S: string): string;
var
  I: Integer;
begin
  I := 1;
  while (I < Length(S)) and (S[I] = '0') do
    Inc(I);
  Result := Copy(S, I, MaxInt);
end;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜