[comp.lang.c] C to Pascal

dan@charyb.COM (Dan Mick) (02/01/90)

In article <1648@milton.acs.washington.edu> pingpong@milton.acs.washington.edu (jimmy) writes:
>Does anyone know how to convert the following C code to Pascal code:
>
>int compute_num(char *word)
>{
>   int num=0;
>   while (*word)
>     num = (num << 5) + *word++;
>   return num;
>}
>

Turbo Pascal?  (strings are different than "std" Pascal).  How about:

TYPE string255: string[255];

function ComputeNum(word:string255):integer;

num:integer;
i:integer;
	
begin
	for i := 1 to Length(word) do
		num := num * 32 + Ord(word[i]);
	ComputeNum := num;
end;

You'll have to subsitute the appropriate crud for strings and lengths
and individual chars of a string otherwise, or form them yourself from
a char array with a length or a null terminator, or some such. The
non-standard strings in Pascal is a really silly thing.
	
		
		

mday@iconsys.icon.com (Matthew T. Day) (02/07/90)

From article <1648@milton.acs.washington.edu>, by pingpong@milton.acs.washington.edu (jimmy):
> Does anyone know how to convert the following C code to Pascal code:
> 
> int compute_num(char *word)
> {
>    int num=0;
>    while (*word)
>      num = (num << 5) + *word++;
>    return num;
> }

How about (just a guess, nothing tested):

type str255 = string[255];
function compute_num(word : str255) : integer;
var
	num, len, x : integer;
begin
	num := 0;
	x := 1;
	len := length(word);

	while (x <= len) do begin
		num := (num shl 5) + ord(word[x]);
		x := x + 1;
	end;
	compute_num := num;
end;

The "shl" operator is available in Turbo Pascal, maybe other Pascal compilers
don't call their shift-left operator "shl", but they should have something.
-- 
          +-------------------------------------------------------+
          | Matthew T. Day, Sanyo/Icon International, Orem, UT    |
          | E-mail: mday@iconsys.icon.com (..!uunet!iconsys!mday) |
          +-------------------------------------------------------+