MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Alberto Fernandez
on 6 Dec 2012

Hi,

i've a little problem(i think). How could i convert binary string like (110010) for example, to "two's" complement? I searched in internet and in this forum, but have not found anything clear.

Is there any function to convert automatically?

I need to convert to two's complement all of this thirty one strings of 16 bits:

Orbital_Inclination =

1111110100111010 1111100111011000 1111110010011010 0000010011010000 1111111000110100 0001010110010111 0010010000111100 0001101101000000 0000000101111000 1101110010001100 0001100111111111 0001101011100111 0001011000110000 0000000010000100 0001101001111111 0000111000101110 1111011011100101 0000110001100100 1111011101001000 1111100010000111 1111010101101101 0000101000101110 0000101011101001 0001001010111001 0001100101000011 0000011001110100 0001100101110101 0000111010001111 0000110011100110 0001100011011111 0000010100111101

I hope that you may help me :)

Thanks!!

*No products are associated with this question.*

Answer by Walter Roberson
on 6 Dec 2012

Accepted answer

Create the number as 16 bit unsigned, uint16() specifically, and then typecast() to 'int16'

Answer by Alberto Fernandez
on 9 Dec 2012

Uff...i'm not solve the problem.

Well, first I concatenated two values:

Orbital_Inclination = strcat(valorsTramaBin_9,valorsTramaBin_10);

the result was the char string of my first message.

Then, i would to convert every row of my first string (yes, i know that its not correct to talk about rows in string) to dec. Everyone of rows of my first message are in two's complement yet.

i try to find a solution to convert everyone of this rows to decimal, but i not know how to separate every row.

For example:

1111110100111010 -->value to convert to dec 1111100111011000 --> another value to convert 1111110010011010 --> idem ...

Could you help me?

Thanks!!

## 5 Comments

## Muruganandham Subramanian (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/55883#comment_115673

did you check this link:

http://www.mathworks.com/help/fixedpoint/ref/bin2num.html

## Alberto Fernandez (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/55883#comment_115676

Thanks Muruganadham!! :)

Let my to try it.

I concatenate two strings of 8 bits each one with thins command:

and then i tried:

then, Matlab says me:

How could i use "bin2num" better for this problem? Where i am wrong?

## Vishal Rane (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/55883#comment_115677

Check the syntax, I believe it requires two inputs. First a quantizer object and second the binary values.

## Alberto Fernandez (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/55883#comment_115682

Ok!

I'm going to try it.

Thanks!

## Walter Roberson (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/55883#comment_115698

Notice that bin2num() is part of the Fixed Point Toolbox.