Converting char arrary to short
From your description it sounds like you want to convert 2 bytes of a char
array into one short. A short in VC 6.0 is 16 bits. A union should easily
do what you want.
short Converted_word = 0;
char MESSAGE[50];
union {
unsigned short WORD;
struct { char LOWER_BYTE;
char UPPER_BYTE; } WORD_INDEX;
} CONVERSION_1;
CONVERSION_1.WORD_INDEX.LOWER_BYTE = MESSAGE [0];
CONVERSION_1.WORD_INDEX.UPPER_BYTE = MESSAGE [1];
Converted_word = CONVERSION_1.WORD;
Or you could also use Bit Shifting to accomplish the same thing.
Converted_word = (short) MESSAGE [0];
Converted_word = Converted_word << 8;
Converted_word = Converted_word | ((short) MESSAGE [1]);
I've read in a char array from a file that should be a bunch for "short"
values.
How do I convert this char array to the short values?
I've tried...
short temp;
sscanf(buff,"%d",&temp);
just to see if the first value is read correctly but it doesn't look like it
is.
I've also tried a lot of other strange things to try and read it but nothing
seems to work.
Can anyone help?
This should be an easy thing to do (or so I thought) - I must be missing
something