Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!elroy.jpl.nasa.gov!decwrl!mcnc!uvaarpa!haven!adm!news From: CDCKAB%EMUVM1.BITNET@cunyvm.cuny.edu ( Karl Brendel) Newsgroups: comp.lang.pascal Subject: Re: Turbo Pascal typecasting and the Mem array. Message-ID: <26155@adm.brl.mil> Date: 27 Feb 91 15:37:27 GMT Sender: news@adm.brl.mil Lines: 63 In article <1991Feb27.033001.2520@ccu.umanitoba.ca>, yackob@eeserv.ee.umanitoba.ca (Zaifu Kerry Yackoboski) wrote: [...deleted] > I'm reading in integers (i.e., signed 16 bit) from a file >and trying to store them in memory using the Mem[segment:offset] >array. My dilemma is that Mem expects a byte (unsigned 8 bit), so I >tried to use the MemW[..] array, except this expects a word (i.e., >unsigned 16 bit). > > Neither Mem nor MemW seems to give me the correct result, >although Mem seems to be somewhat close (when I plot out the data, >it has the right shape but the wrong values). > > I think a possible solution might be a value type-cast of >the sort > > MemW[seg:offset] := Word (my_integer); > >but the explanation in the reference isn't clear; it says the sign >is always preserved when the value is extended, whatever that means. >I'll try this, but I hate to just muck about and hope to find the >correct answer. Try that, or jump directly to Move(my_integer,MemW[segment:offset],SizeOf(my_integer)); > Another aspect of the problem that I'm hazy about (and >therefore this might be the real problem) is I'm unsure about how to >increment the offset with the MemW[seg:offset] array; should I be >increasing by two's, since there are 2 bytes per array element, or >does the compiler do that for me, allowing me to increment by one's >only ? By 2. Better, by SizeOf(my_integer). But watch out for the end of the segment. As an alternative approach, declare a type that is a pointer to an array of integer, then assign to it the address of the first element of interest in MemW, then just deal with that array (pointed to by your pointer), thus only worrying about the index of the array rather than segment arithmetic. E.g., if seg0 and ofs0 are the segment and offset of that first element: type tIntArrayPtr = ^tIntArray; tIntArray = array[0..0]of integer; var p : tIntArrayPtr; seg0,ofs0 : word; i : integer; begin p := Ptr(seg0,ofs0); p^[0] := i; (Note the declaration of tIntArray as array[0..0]. You can do this and turn range checking off where you reference elements beyond the first, or you can assign some different bounds for the index, as you prefer.)