Recent

Author Topic: is there a Base 26 / Radix 26 A..Z functions in the libs?  (Read 886 times)

Josh

  • Hero Member
  • *****
  • Posts: 1460
Re: is there a Base 26 / Radix 26 A..Z functions in the libs?
« Reply #15 on: June 08, 2026, 08:54:36 pm »
Here some code i done a while ago, when analyzing massive xls sheets.
think it handles 10+letters.

coltonum does bitshifting 16 8 2, and alsor and to uppercase. should be pretty quick

not 100% tested, but it did wat i wanted at the time

Code: Pascal  [Select][+][-]
  1. function ColumnToNumber(const colStr:ShortString):UInt64;
  2. var
  3.   i:UInt32;
  4.   strBytes:array[0..255] of Byte absolute colStr;
  5. begin
  6.   Result:=0;
  7.   for i:=1 to strBytes[0] do     // shortstring so byte 0 is length
  8.   begin
  9.     //bit shifting 16 8 2 to get 26, and $DF to uppercase byte
  10.     Result:=(Result shl 4)+(Result shl 3)+(Result shl 1)+UInt64((strBytes[i] and $DF)-64);
  11.   end;
  12. end;
  13.  
  14. function NumberToColumn(num:UInt64):ShortString;
  15. var
  16.   temp: array[0..15] of Byte;
  17.   idx,i: UInt32;
  18.   remainder: UInt32;
  19. begin
  20.   if num=0 then exit('');
  21.   idx:=0;
  22.   while num>0 do
  23.   begin
  24.     Dec(num);
  25.     remainder:=num mod 26;
  26.     num:=num div 26;
  27.     temp[idx]:=remainder+65;
  28.     Inc(idx);
  29.   end;
  30.   Byte(Result[0]):=idx;
  31.   for i:=0 to idx-1 do
  32.   begin
  33.     Byte(Result[i+1]):=temp[(idx-1)-i];
  34.   end;
  35. end;
« Last Edit: June 09, 2026, 12:21:27 am by Josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

 

TinyPortal © 2005-2018