I am trying to read bytes from a file. So I open the file, read 1 byte, and read another byte.
Then I print the read bytes through my own logging functions. And I get bizarre numbers written to the log.
The first number read from the file is 0xB2 and is printed as -78 to the log. That is ok since the variable that holds the read byte is a signed 8 bit variable. But the next number that is read is stored as 0x80 in the file, but printed as -3 in the log. This is very strange to me. Why is it not printed as -128, as you’d expect?
In any case, while digging around in the LMS2012 source I found this snippet:
// VM DATA TYPES typedef SBYTE DATA8; //!< VM Type for 1 byte signed value typedef SWORD DATA16; //!< VM Type for 2 byte signed value typedef SLONG DATA32; //!< VM Type for 4 byte signed value typedef FLOAT DATAF; //!< VM Type for 4 byte floating point value // VM VARIABLE TYPES typedef UBYTE VARDATA; //!< Variable base type typedef UBYTE IMGDATA; //!< Image base type typedef UWORD PRGID; //!< Program id type typedef UWORD OBJID; //!< Object id type typedef IMGDATA* IP; //!< Instruction pointer type typedef VARDATA* LP; //!< Local variable pointer type typedef VARDATA* GP; //!< global variable pointer type typedef ULONG IMINDEX; //!< ImageData index type typedef ULONG GBINDEX; //!< GlobalBytes index type typedef ULONG LBINDEX; //!< LocalBytes index type typedef UWORD TRIGGER; //!< TriggerCount type typedef UBYTE PARS; //!< NoOfParameters type typedef SLONG IMOFFS; //!< ImageData offset type typedef DATA16 HANDLER; //!< Memory list index
So it seems there are no unsigned numbers exposed to the VM. So if I read 0xB2 from file, it will forever be stored as -78, even when moved from a signed 8 bit variable to a signed 16 bit variable. Unless I convert it myself of course, but there is no built in converting from signed to unsigned so I need to make my own functions to handle that too.