C to Pascal

Matthew T. Day mday at iconsys.icon.com
Wed Feb 7 08:06:25 AEST 1990


>From article <1648 at milton.acs.washington.edu>, by pingpong at 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 at iconsys.icon.com (..!uunet!iconsys!mday) |
          +-------------------------------------------------------+



More information about the Comp.lang.c mailing list