My short int likes 0xffff9001
Hello. I have a bizarre problem dealing with converting an int to a
short int. I read a line from my datafile(ex 1.1) as an int (in hex)
and attempt to format it into a short int. When I do this, I get what
looks like bizarre integral promotion in the output of UNIT.Whole and
HOUSE.Whole. I should point out, that I am attempting to mask out the
upper bits, but I still get shot in the foot and the (so labeled) really
important sprintf statement produces 0xffff9001 for UNIT.Whole and
0xffff8002 for House.Whole.
I should note that it is important that I use the union to do this...
and that even if I just define UNIT a short int (removing the unions)
that I receive similar bad answers.
Any help would be appreciated. Look at the code carefully...
Benjamin G. Haupt
Foster-Miller, Inc.
Staff Engineer
ex 1.1
/* Typical Datafile line */
// UNIT // HOUSE // IGNORE
0x9001 0x8002 0x0000000000000000000
I have defined a macro D_PRINT and a union in the header file.
ex 1.2
/* FROM THE .h FILE */
#ifdef DEBUG
#define D_PRINT(x) fprintf(stderr, "DEBUG: %s\n", x); fflush(stderr);
#else
#define D_PRINT(x)
#endif
typedef union
{
unsigned char SigByte[2];
/* SigByte[0] LSB
SigByte[1] MSB */
short int Whole;
Quote:
} ConvByte;
In my main routine...
ex 1.3
/* FROM THE .c FILE */
for(int i = 0; i<n; i++)
{
int t_unit;
ConvByte UNIT;
int t_house;
ConvByte HOUSE;
GetLine(address_list, buffer);// Reads a line from datafile
// places it in buffer
// omitted for conciseness
sscanf(buffer, "%x\t%x", &t_unit, &t_house);
D_PRINT(buffer);
/* REALLY IMPORTANT LINES */
UNIT.Whole = (short int)(t_unit & 0x0000FFFF);
HOUSE.Whole = (short int)(t_house & 0x0000FFFF);
sprintf(debug_msg,"%#04x %#04x", UNIT.Whole, HOUSE.Whole);
/* END REALLY IMPORTANT LINES */
D_PRINT(debug_msg);
sprintf(debug_msg,"%#02x %#02x",UNIT.SigByte[0],
UNIT.SigByte[1]);
D_PRINT(debug_msg);
}