% From the book % PROLOG PROGRAMMING IN DEPTH % by Michael A. Covington, Donald Nute, and Andre Vellino % (Prentice Hall, 1997). % Copyright 1997 Prentice-Hall, Inc. % For educational use only % File READI16.PL % Routines to read 16-bit binary integers from a file % Assumes less significant byte comes first % (as on PC's, not Sparcstations). % If this is not the case, swap Lo and Hi in read_u16/1. :- ensure_loaded('readbyte.pl'). % or use reconsult if necessary % read_u16(-Integer) % Reads 2 bytes as a 16-bit unsigned integer, LSB first. read_u16(I) :- % 16-bit unsigned integer read_bytes(2,[Lo,Hi]), I is Hi*256 + Lo. % read_i16(-Integer) % Reads 2 bytes as a 16-bit signed integer, LSB first. read_i16(I) :- % 16-bit signed integer read_u16(U), u16_to_i16(U,I). % u16_to_i16(+U,-I) % Converts 16-bit unsigned to signed integer. u16_to_i16(U,I) :- U > 32767, !, I is U - 65536. u16_to_i16(U,U).