Recent

Author Topic: Decimal <=> Dozinal (Duodecimal) Conversion Functions  (Read 2279 times)

Weosule

  • Newbie
  • Posts: 5
Decimal <=> Dozinal (Duodecimal) Conversion Functions
« on: February 20, 2019, 05:40:40 pm »
 %)
Hi...

I'm hoping that someone might have code to convert decimal numbers to duodecimal  numbers and back and would be willing to share. I built my own function for decimal to duodecimal but it does it with a lookup table that is good for decimal numbers from 0 to 100. I need something for higher values. I also need to handle fractional values. If no one has code then some suggestions on how to tackle this problem to build improved functions would be welcome.

Thank you all in advance

Weosule

Bart

  • Hero Member
  • *****
  • Posts: 5274
    • Bart en Mariska's Webstek
Re: Decimal <=> Dozinal (Duodecimal) Conversion Functions
« Reply #1 on: February 20, 2019, 06:04:49 pm »
You can take a look at the JavaScript source code in http://flud.org/dozenal-calc.html.

On this page you can find an algorith to do base conversions from and to decimal for any base.

Bart
« Last Edit: February 20, 2019, 11:00:10 pm by Bart »

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: Decimal <=> Dozinal (Duodecimal) Conversion Functions
« Reply #2 on: February 20, 2019, 08:19:17 pm »

algorithm for unsinged integer (at the moment)
base choices 2 to 36
Code: Pascal  [Select][+][-]
  1.    program bases;
  2.  
  3.    var
  4.    u,i,n:longword;
  5.    base:byte;
  6.    s:pchar;
  7.                                                                        //libc.so linux
  8.  function  ultoa(T:longword;F:pointer;sz:integer):pchar ;cdecl external 'msvcrt.dll' name '_ultoa';
  9.  function strtoul (s:pchar;b:pointer;L: longint):longword ;cdecl external 'msvcrt.dll' name 'strtoul';
  10.  
  11.  
  12.  function uintToBase(N:longword;_base:byte):pchar;
  13.  var
  14.    s:ansistring;
  15.     buffer:pchar;
  16.     begin
  17.     setlength(s,50);
  18.     buffer:=@s[1];
  19.     ultoa(n,buffer,_base);
  20.     result:= (buffer)
  21. end;
  22.  
  23.  function uintFromBase(s:pchar;_base:byte):longword ;
  24.     begin
  25.     result:= strtoul(s,nil,_base);
  26. end;
  27.  
  28.  
  29. begin
  30. base:=12;
  31. for i:=1 to 40 do
  32. begin
  33. n:=random(5000000);
  34. s:=uintTobase(n,base);
  35. u:=uintFromBase(s,base);
  36.  
  37. writeln('num = ',n,'  base ',base,' = ',s,'   return = ',u,'   compare ',n=u );
  38. end;
  39.   writeln('press enter to end . . .');
  40. readln;
  41. end.
For signed floats a project beckons.
Sorry for the un formatted writeln

Dev-Pas ide. tested 32 and 64 bits fp 3.04

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Decimal <=> Dozinal (Duodecimal) Conversion Functions
« Reply #3 on: February 20, 2019, 08:21:37 pm »
Take a look at the attached applications.

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: Decimal <=> Dozinal (Duodecimal) Conversion Functions
« Reply #4 on: February 21, 2019, 12:43:28 am »

Try some floats (singles) using a union.
Code: Pascal  [Select][+][-]
  1.  
  2.    program bases;
  3.  
  4.    var
  5.    u,i,n:longword;
  6.    base:byte;
  7.    s:pchar;
  8.    g:string;
  9.    f,r:single;
  10.  
  11.                                                                        //libc.so linux
  12.  function  ultoa(T:longword;F:pointer;sz:integer):pchar ;cdecl external 'msvcrt.dll' name '_ultoa';
  13.  function strtoul (s:pchar;b:pointer;L: longint):longword ;cdecl external 'msvcrt.dll' name 'strtoul';
  14.  
  15.  
  16.  function uintToBase(N:longword;_base:byte):pchar;
  17.  var
  18.    s:ansistring;
  19.     buffer:pchar;
  20.     begin
  21.     setlength(s,50);
  22.     buffer:=@s[1];
  23.     ultoa(n,buffer,_base);
  24.     result:= (buffer)
  25. end;
  26.  
  27.  function uintFromBase(s:pchar;_base:byte):longword ;
  28.     begin
  29.     result:= strtoul(s,nil,_base);
  30. end;
  31.  // =====  singles  =====
  32.  //create a union
  33. type
  34. switch =record
  35. case integer of
  36. 0:(float:single);
  37. 1:(value:longword);
  38. end;
  39.  
  40. function floattobase(n:single;b:byte):pchar ;
  41. var
  42. z:switch;
  43. begin
  44.     z.float:=n;
  45.     result:= uinttobase(z.value,b);
  46. end;
  47.  
  48. function floatfrombase(s:string;b:byte):single ;
  49. var
  50. z:switch;
  51. p:pchar;
  52. begin
  53. p:=@s[1] ;
  54.     z.value:=uintfrombase(p,b);
  55.     result:= z.float;
  56. end;
  57.  
  58.  
  59.  
  60.  
  61. begin
  62. base:=12;
  63. for i:=1 to 15 do
  64. begin
  65. n:=random(5000000);
  66. s:=uintTobase(n,base);
  67. u:=uintFromBase(s,base);
  68.  
  69. writeln('num = ',n,'  base ',base,' = ',s,'   return = ',u,'   compare ',n=u );
  70. end;
  71.  
  72.  
  73. writeln;
  74.  
  75.  for i:=1 to 15 do
  76. begin
  77.   r:=random* 1000000-random*1000000;
  78. s:=floattobase(r,base);
  79. f:=floatfrombase(s,base);
  80. writeln('num = ',r,'  base ',base,' = ',s,'   return = ',f,'   compare ',r=f );
  81.  
  82. end;
  83. writeln('press enter to end . . .');
  84. readln;
  85. end.
dev-pas ide  tested 32 and 64 bit fp 3.04

 

TinyPortal © 2005-2018