程序如图。
目的把十进制的值转成N进制。源码如下:
点击按钮后在按钮事件
sSeq:=GetSeqByCarry(iNum,iJinZhi,iLength,sFormat);
lbResult.caption:=sSeq;GetSeqByCarry(十进制值,需要转的进制,需要显示的长度,目标进制的取值范围)function GetSeqByCarry(iSeq, iCarry,iLength: Integer; sFormat: string): string; //秈锣ヴ種秈
var
iMod: Integer;
begin
Result := '';
iMod := 0;
if (iSeq = 0) then
Result := '0'
else
begin
while(iSeq > 0 ) do
begin
iMod := iSeq mod iCarry;
Result := sFormat[1 + iMod] + Result; //很关键的部分具体思路见下图
iSeq := iSeq div iCarry;
end;
end; //拼接长度 这里不展示了
end;有好的经验大家互相交流分享。可以在下面留言。
目的把十进制的值转成N进制。源码如下:
点击按钮后在按钮事件
sSeq:=GetSeqByCarry(iNum,iJinZhi,iLength,sFormat);
lbResult.caption:=sSeq;GetSeqByCarry(十进制值,需要转的进制,需要显示的长度,目标进制的取值范围)function GetSeqByCarry(iSeq, iCarry,iLength: Integer; sFormat: string): string; //秈锣ヴ種秈
var
iMod: Integer;
begin
Result := '';
iMod := 0;
if (iSeq = 0) then
Result := '0'
else
begin
while(iSeq > 0 ) do
begin
iMod := iSeq mod iCarry;
Result := sFormat[1 + iMod] + Result; //很关键的部分具体思路见下图
iSeq := iSeq div iCarry;
end;
end; //拼接长度 这里不展示了
end;有好的经验大家互相交流分享。可以在下面留言。
procedure TForm1.Button1Click(Sender: TObject);
const
Keys: array[0..61] of AnsiChar = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
MAX_RCOUNT = 64;
var
x, lCount: uint8;
lNum, lM: UInt64;
lStr, lResult: AnsiString;
begin
lNum := Random(10000000);
x := Random(62 - 16) + 16;
Memo1.Lines.Clear;
Memo1.Lines.Add('进制: ' + x.ToString);
Memo1.Lines.Add('10进制: ' + lNum.ToString); SetLength(lStr, MAX_RCOUNT);
lCount := 0;
while lNum >= x do
begin
lM := lNum mod x;
lStr[MAX_RCOUNT - lCount] := Keys[lM];
Inc(lCount);
lNum := lNum div x;
end;
lStr[MAX_RCOUNT - lCount] := Keys[lNum];
Inc(lCount); SetLength(lResult, lCount);
Move(lStr[MAX_RCOUNT - lCount + 1], lResult[1], lCount);
Memo1.Lines.Add(Format('%d进制: %s', [x, lResult]));
end;