From: Thanushka on
Hi,
I am currently doing a project and am a novice to Matlab. I have a problem where i receive serial 'dataframes', each consisting of two 8-bit values (the high byte and the low byte), which have to be constructed to get a 16-bit value. This assembled value is already in two's complement and I need to get the original value.
Any help is appreciated. Thanks in advance.
From: Walter Roberson on
Thanushka wrote:

> I am currently doing a project and am a novice to Matlab. I have a
> problem where i receive serial 'dataframes', each consisting of two
> 8-bit values (the high byte and the low byte), which have to be
> constructed to get a 16-bit value. This assembled value is already in
> two's complement and I need to get the original value. Any help is
> appreciated. Thanks in advance.

typecast([uint8(HighByte), uint8(LowByte)], 'int16')

On some systems you may need to reverse the high and low bytes.
From: Thanushka on
Walter Roberson <roberson(a)hushmail.com> wrote in message <htmp2a$48f$1(a)canopus.cc.umanitoba.ca>...
> Thanushka wrote:
>
> > I am currently doing a project and am a novice to Matlab. I have a
> > problem where i receive serial 'dataframes', each consisting of two
> > 8-bit values (the high byte and the low byte), which have to be
> > constructed to get a 16-bit value. This assembled value is already in
> > two's complement and I need to get the original value. Any help is
> > appreciated. Thanks in advance.
>
> typecast([uint8(HighByte), uint8(LowByte)], 'int16')
>
> On some systems you may need to reverse the high and low bytes.

Thanks but by the original value i meant the value before taking the two's complement. My original post may not have been clear. Sorry.
eg: LowByte = 0xFE HighByte = 0xFF
Answer should equal -2
From: Walter Roberson on
Thanushka wrote:
> Walter Roberson <roberson(a)hushmail.com> wrote in message
> <htmp2a$48f$1(a)canopus.cc.umanitoba.ca>...
>> Thanushka wrote:
>>
>> > I am currently doing a project and am a novice to Matlab. I have a >
>> problem where i receive serial 'dataframes', each consisting of two >
>> 8-bit values (the high byte and the low byte), which have to be >
>> constructed to get a 16-bit value. This assembled value is already in
>> > two's complement and I need to get the original value.

>> typecast([uint8(HighByte), uint8(LowByte)], 'int16')
>>
>> On some systems you may need to reverse the high and low bytes.

> Thanks but by the original value i meant the value before taking the
> two's complement. My original post may not have been clear. Sorry. eg:
> LowByte = 0xFE HighByte = 0xFF Answer should equal -2

As I said, you may need to reverse the bytes on some systems.

>> typecast(uint8([hex2dec('FE'),hex2dec('FF')]),'int16')
ans =
-2


Hmmm, looking further, it appears that Matlab is now only offered for "big
endian" systems, so reversing the bytes is possibly needed on all current
Matlab systems. Still, there are plenty of people around running older Matlab
releases, possibly on systems that are little-endian .
From: James Tursa on
"Thanushka " <galazz442(a)gmail.com> wrote in message <htmpva$91s$1(a)fred.mathworks.com>...
> Walter Roberson <roberson(a)hushmail.com> wrote in message <htmp2a$48f$1(a)canopus.cc.umanitoba.ca>...
> > Thanushka wrote:
> >
> > > I am currently doing a project and am a novice to Matlab. I have a
> > > problem where i receive serial 'dataframes', each consisting of two
> > > 8-bit values (the high byte and the low byte), which have to be
> > > constructed to get a 16-bit value. This assembled value is already in
> > > two's complement and I need to get the original value. Any help is
> > > appreciated. Thanks in advance.
> >
> > typecast([uint8(HighByte), uint8(LowByte)], 'int16')
> >
> > On some systems you may need to reverse the high and low bytes.
>
> Thanks but by the original value i meant the value before taking the two's complement. My original post may not have been clear. Sorry.
> eg: LowByte = 0xFE HighByte = 0xFF
> Answer should equal -2

Using Walter's suggestion on a PC (Little Endian):

>> LowByte = hex2dec('FE')
LowByte =
254
>> HighByte = hex2dec('FF')
HighByte =
255
>> typecast([uint8(LowByte), uint8(HighByte)], 'int16')
ans =
-2

This appears to give what you want if the inputs are double class. What class variable are your original values in? double or int8 or uint8? If they are int8 or uint8 already then you can dispense with the uint8 casts above and just use (on a Little Endian system):

typecast([LowByte, HighByte], 'int16')


James Tursa