魔力封包数字通讯之62进制

来源: 作者: 点击:
力封包,在传输数字时候采用62进制,比如坐标,NPCID等等数字,目的是为了减少传输量。
下面贴出2个采用delphi编写的转换函数
10进制转62进制

function c10to62(iDex: integer): string;
var
s: array[0..61] of string;
i: integer;
begin
for i := 0 to 9 do
s := inttostr(i);
for i := 10 to 35 do
begin
s := chr(i + 87);
end;
for i := 36 to 61 do
begin
s := chr(i + 29);
end;
result := '';
while iDex <> 0 do
begin
result := s[iDex mod 62] + result;
iDex := iDex div 62;
end;
end;


62进制转10进制

function c62to10(s62: string): integer;
var
s: array[0..61] of string;
i, j: integer;
sTmp: string;
nTmp: integer;
begin
for i := 0 to 9 do
s := inttostr(i);
for i := 10 to 35 do
begin
s := chr(i + 87);
end;
for i := 36 to 61 do
begin
s := chr(i + 29);
end;
result := 0;
for i := 1 to length(s62) do
begin
sTmp := copy(s62, (i), 1);
nTmp := -1;
for j := 0 to 61 do
if s[j] = sTmp then
begin
nTmp := j;
break;
end;
if nTmp = -1 then
begin
result := -1;
exit;
end;
result := result * 62 + nTmp;
end;
end;
栏目列表