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.
|
|
0
|
|
|
|
Reply
|
Thanushka
|
5/27/2010 9:38:03 PM |
|
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.
|
|
0
|
|
|
|
Reply
|
Walter
|
5/27/2010 9:42:38 PM
|
|
Walter Roberson <roberson@hushmail.com> wrote in message <htmp2a$48f$1@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
|
|
0
|
|
|
|
Reply
|
Thanushka
|
5/27/2010 9:59:06 PM
|
|
Thanushka wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message
> <htmp2a$48f$1@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 .
|
|
0
|
|
|
|
Reply
|
Walter
|
5/27/2010 10:12:27 PM
|
|
"Thanushka " <galazz442@gmail.com> wrote in message <htmpva$91s$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <htmp2a$48f$1@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
|
|
0
|
|
|
|
Reply
|
James
|
5/27/2010 10:23:04 PM
|
|
Walter Roberson <roberson@hushmail.com> wrote in message <htmqqd$6vm$1@canopus.cc.umanitoba.ca>...
>
> Hmmm, looking further, it appears that Matlab is now only offered for "big
> endian" systems
Huh? PC's are Little Endian.
James Tursa
|
|
0
|
|
|
|
Reply
|
James
|
5/27/2010 10:25:06 PM
|
|
James Tursa wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message
> <htmqqd$6vm$1@canopus.cc.umanitoba.ca>...
>>
>> Hmmm, looking further, it appears that Matlab is now only offered for
>> "big endian" systems
>
> Huh? PC's are Little Endian.
I knew something looked wrong as I was typing it, sigh.
|
|
0
|
|
|
|
Reply
|
Walter
|
5/27/2010 10:39:28 PM
|
|
"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <htmrg2$fuo$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <htmqqd$6vm$1@canopus.cc.umanitoba.ca>...
> >
> > Hmmm, looking further, it appears that Matlab is now only offered for "big
> > endian" systems
>
> Huh? PC's are Little Endian.
>
> James Tursa
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.
|
|
0
|
|
|
|
Reply
|
Thanushka
|
5/27/2010 10:43:06 PM
|
|
Walter Roberson <roberson@hushmail.com> wrote in message <htmqqd$6vm$1@canopus.cc.umanitoba.ca>...
> Thanushka wrote:
> > Walter Roberson <roberson@hushmail.com> wrote in message
> > <htmp2a$48f$1@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.
|
|
0
|
|
|
|
Reply
|
Thanushka
|
5/27/2010 10:49:05 PM
|
|
"Thanushka " <galazz442@gmail.com> wrote in message <htmshq$lmk$1@fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <htmrg2$fuo$1@fred.mathworks.com>...
> > Walter Roberson <roberson@hushmail.com> wrote in message <htmqqd$6vm$1@canopus.cc.umanitoba.ca>...
> > >
> > > Hmmm, looking further, it appears that Matlab is now only offered for "big
> > > endian" systems
> >
> > Huh? PC's are Little Endian.
> >
> > James Tursa
>
>
> 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 how to implement this in C#?
Thanks in advance....
|
|
0
|
|
|
|
Reply
|
Thanushka
|
5/28/2010 10:00:23 AM
|
|
"Thanushka " <galazz442@gmail.com> wrote in message <htmst1$d6j$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <htmqqd$6vm$1@canopus.cc.umanitoba.ca>...
> > Thanushka wrote:
> > > Walter Roberson <roberson@hushmail.com> wrote in message
> > > <htmp2a$48f$1@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.
|
|
0
|
|
|
|
Reply
|
Thanushka
|
5/28/2010 10:01:05 AM
|
|
"Thanushka " <galazz442@gmail.com> wrote in message <hto491$4ll$1@fred.mathworks.com>...
> "Thanushka " <galazz442@gmail.com> wrote in message <htmst1$d6j$1@fred.mathworks.com>...
> > Walter Roberson <roberson@hushmail.com> wrote in message <htmqqd$6vm$1@canopus.cc.umanitoba.ca>...
> > > Thanushka wrote:
> > > > Walter Roberson <roberson@hushmail.com> wrote in message
> > > > <htmp2a$48f$1@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
|
|
0
|
|
|
|
Reply
|
Steve
|
5/28/2010 10:35:04 AM
|
|
"Walter Roberson" <roberson@hushmail.com> wrote in message
news:htmqqd$6vm$1@canopus.cc.umanitoba.ca...
> Thanushka wrote:
>> Walter Roberson <roberson@hushmail.com> wrote in message
>> <htmp2a$48f$1@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@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
|
|
0
|
|
|
|
Reply
|
Steven
|
5/28/2010 2:15:13 PM
|
|
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);
|
|
0
|
|
|
|
Reply
|
Walter
|
5/28/2010 3:16:41 PM
|
|
|
13 Replies
453 Views
(page loaded in 0.591 seconds)
Similiar Articles: 16 bit two's complement - comp.soft-sys.matlabHi, 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 valu... two's complement 8 bit data to decimal - comp.soft-sys.matlab ...16 bit two's complement - comp.soft-sys.matlab two's complement 8 bit data to decimal - comp.soft-sys.matlab ... 16 bit two's complement - comp.soft-sys.matlab two's ... Convert signed integer to a 16-bit hex number - comp.soft-sys ...I need to convert a signed integer to a 16-bit sign-extended (2's complement) hex number for output to a file. For example: 3 -> 0003 -3 -> FFFD I've tried ... VHDL code for 2's complement - comp.lang.vhdl16 bit two's complement - comp.soft-sys.matlab VHDL code for 2's complement - comp.lang.vhdl I need help writing some code that will subtract 2 8-bit numbers by using ... Help! Signed Number Representation in Xilinx Testbench Waveform ...16 bit two's complement - comp.soft-sys.matlab Help! Signed Number Representation in Xilinx Testbench Waveform ... two's complement 8 bit data to decimal - comp.soft-sys ... Signed multiplication - comp.lang.vhdlHow multiply two __int64 ... - comp.lang.c Long ago I used a language that had two integer multiplication operators: * multiplied two signed two's complement 16-bit ... How multiply two __int64 ... - comp.lang.cLong ago I used a language that had two integer multiplication operators: * multiplied two signed two's complement 16-bit numbers and gave a product in ... Compiler for 16bit architecture - comp.compilers.lcc16 bit two's complement - comp.soft-sys.matlab Compiler for 16bit architecture - comp.compilers.lcc... lang.c Compile it with debug info and start the debugger ... replace hex sequence - comp.lang.awk16 bit two's complement - comp.soft-sys.matlab Hex to decimal replacement - comp.programming replace hex sequence - comp.lang.awk I gave up on trying a multi-char hex ... int32 to double - comp.soft-sys.matlab16 bit two's complement - comp.soft-sys.matlab int32 to double - comp.soft-sys.matlab 16 bit two's complement - comp.soft-sys.matlab int32 to double - comp.soft-sys.matlab ... Ping from C tutorial, problem - comp.compilers.lcc16 bit two's complement - comp.soft-sys.matlab Ping from C tutorial, problem - comp.compilers.lcc // memset(datapart, 'A', datasize); } // // Function: checksum ... CRC, add 16 parity bits in MATLAB - comp.soft-sys.matlab ...CRC, add 16 parity bits in MATLAB - comp.soft-sys.matlab ..... About CRC-32 checksum - comp.compression ... 16 bit two's complement - comp.soft-sys.matlab... it appears ... Signed shift of 32-bit int using 16-bit instructions? - comp.lang ...I'm working on a hobby project in 16-bit DOS and trying to figure out how to do a signed shift left of a 32-bit signed integer in DX:AX. I have alrea... default value for bit - comp.databases.mysql... 12/31/2007 11:16:40 AM ... to encode an unconditional jump in 64-bit mode? - comp.lang ... default value for bit ... lcc compiler is missing in windows 7(32 bit) in matlab 2007b ...Ping from C tutorial, problem - comp.compilers.lcc "The ping.exe file is linked to missing ... 16 bit two's complement - comp.soft-sys.matlab Ping from C tutorial, problem ... Two's complement - Wikipedia, the free encyclopediaAn N-bit two's-complement numeral system can represent every integer in the range −(2 N −1 ... any possible sum (e.g., 5 bits can represent values in the range −16 to 15 ... How do I do 16-bit 2's complement? - Yahoo! AnswersBest Answer: Sounds correct to me. If you had a negative 8-bit number and needed to convert it to 16-bit you do what is called sign extension where you ... 7/25/2012 6:32:19 PM
|