Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Determining the length of "long"?



On Sep 11, 10:36pm, Dominik Vogt wrote:
}
} Is there a way to determine the length of the C type long from
} inside a zsh script (without using external programs, of course).

As Kurtis mentioned, the shell really isn't the right tool for this,
but:

if (( ${#:-"$(( [#2] (1<<31)))"} > ${#:-"$(( [#2] (1<<63)))"} ))
then print "zsh integer type is 32 bits"
elif (( ${#:-"$(( [#2] (1<<63)))"} > ${#:-"$(( [#2] (1<<64)))"} ))
then print "zsh integer type is 64 bits"
else print "zsh integer type is more than 64 bits"
fi

} As an alternative, is there a direct way to print out a "long"
} integer value as binary bytes in host byte order?

There's no guarantee that zsh's integer type is "long", so no, there
is not.  You can try fiddling with bitwise operators, e.g.:

setopt C_BASES
integer i=0x12345
while (( i )) {
  printf '\\\\x%x\n' $(( [#16] (i & 0xff) ))
  (( i = i >> 8 ))
}

Printing the zero bytes and not messing up the byte order is left as
an exercise for the reader.



Messages sorted by: Reverse Date, Date, Thread, Author