首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

怎么将数字转为字母

2012-03-08 
如何将数字转为字母?如:1A,2B...27AA,28AB这样的,如何做呢,有函数吗?[解决办法]数字0怎么处理?只好返

如何将数字转为字母?
如:1=A,2=B...27=AA,28=AB
这样的,如何做呢,有函数吗?

[解决办法]
数字0怎么处理?只好返回'0'。

Delphi(Pascal) code
function IntToAlpha(Value: Integer): string;const  alphas: array[1..26] of char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';var  Negative: Boolean;  s: array[1..10] of char;  i, v: Integer;begin  if Value = 0 then  begin    Result := '0';    Exit;  end;  Negative := Value < 0;  if Negative then    Value := -Value;  i := 10;  repeat    v := Value mod 26;    if v = 0 then    begin      v := 26;      Dec(Value);    end;    s[i] := alphas[v];    Dec(i);    Value := Value div 26;  until Value <= 0;  if Negative then  begin    s[i] := '-';    Dec(i);  end;  SetLength(Result, 10 - i);  Move(s[i+1], Result[1], 10 - i);end;
[解决办法]
Delphi(Pascal) code
//用这个函数:function IntToLetter(Value: Integer): String;begin  Result := '';  while Value > 0 do  begin    Result := chr(ord('A') + (Value - 1) mod 26) + Result;    Value := (Value - 1) div 26;  end;end;//测试的例子:procedure TForm1.FormCreate(Sender: TObject);begin  ShowMessage (IntToLetter(1));  ShowMessage (IntToLetter(26));  ShowMessage (IntToLetter(27));  ShowMessage (IntToLetter(52));  ShowMessage (IntToLetter(53));end; 

热点排行