From: Thanushka on
"Thanushka " <galazz442(a)gmail.com> wrote in message <htmst1$d6j$1(a)fred.mathworks.com>...
> Walter Roberson <roberson(a)hushmail.com> wrote in message <htmqqd$6vm$1(a)canopus.cc.umanitoba.ca>...
> > 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 .
>
> I'm running Matlab R2009a and this solution seems to be working fine. The data is stored in a matrix which seems to default to double data type. Thanks a lot to both replies. Much appreciated.

Maybe unrelated but any idea on how to implement this in C#?
Thanks in advance.
From: Steve Amphlett on
"Thanushka " <galazz442(a)gmail.com> wrote in message <hto491$4ll$1(a)fred.mathworks.com>...
> "Thanushka " <galazz442(a)gmail.com> wrote in message <htmst1$d6j$1(a)fred.mathworks.com>...
> > Walter Roberson <roberson(a)hushmail.com> wrote in message <htmqqd$6vm$1(a)canopus.cc.umanitoba.ca>...
> > > 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 .
> >
> > I'm running Matlab R2009a and this solution seems to be working fine. The data is stored in a matrix which seems to default to double data type. Thanks a lot to both replies. Much appreciated.
>
> Maybe unrelated but any idea on how to implement this in C#?
> Thanks in advance.

You may need to get your hands dirty and work on the bits directly. Use shift and or operators to assemble your int from the two bytes. Then subtract one and flip the bits.

Some nice person has explained it here: http://en.wikipedia.org/wiki/Two's_complement
From: Steven Lord on

"Walter Roberson" <roberson(a)hushmail.com> wrote in message
news:htmqqd$6vm$1(a)canopus.cc.umanitoba.ca...
> 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 .

And following up on this, you can use the third output of the COMPUTER
function to determine the endian-ness of the machine on which your code is
running.

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/computer.html

You can use this to decide whether or not to reverse the bytes ... although
now that I look at it, the table indicates all the currently-supported
platforms are little endian. I guess some of the platforms that we used to
support but no longer do were big endian.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com


From: Walter Roberson on
Thanushka wrote:

> Maybe unrelated but any idea how to implement this in C#?

I have not looked at C#. In C or C++, I would use a union, preferably
with one of the data types of fixed width (C99, not supported in C89).
Something like,

union {
struct {
uint_8 low_byte;
uint_8 high_byte;
} as_bytes;
int_16 as_word;
} word_and_bytes;

word_and_bytes combineword;

combineword.as_bytes.low_byte = '\xfe';
combineword.as_bytes.high_byte = '\xff';
sprintf('\d\n', combineword.as_word);