cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X

How to convert from binary number (vector form) to decimal (with Prime)?

rking-2
1-Newbie

How to convert from binary number (vector form) to decimal (with Prime)?

Greetings.

I need convert binary numbers and show the process, for example:

The binary 1011 is the matrix vector [1 0 1 1], need a program that make 2^3, 0^2, 2^1 and 2^0 then show a matrix [8 0 2 1] and later the sum 11 (8+2+1).

Need show the matrix and sum, working according to the number of digits.

Also need convert (based on block of 3 and 4 digits of) binary number to octal and hexa numbers.

1 ACCEPTED SOLUTION

Accepted Solutions

Valery Ochkov wrote:

Sorry.

for i=ORIGIN to last(x) - ORIGIN

NO! Definitely not. That would be one or more too few, if ORIGIN>0.

But thanks, because you are right, that I made a mistake in this respect ( I should have tried using a value for ORIGIN different to 0).

We can correct it in different way. I guess the way I have chosen in cdec2 is the better one:

As a bonus I added the ability to provide the argument either as a row vector OR as a simple scalar.

Werner

View solution in original post

7 REPLIES 7

The problem is, that when you use the column-selector, you get a 1x1 matrix, bot a scalar.

You can't compare a 1x1 matrix to a scalar like 1 or 0.

Use something like x[0,i instead.

Furthermore you program has no return value and the while loop will rund endless, as i never changes inside that loop.

Not clear what you try to do with x[i]<--x[i] in that "try and catch".

WE

May be so?

Looks like you also want to see the powers of 2 as intermediate result and you also tried to implement some error handling.

Maybe you were looking for something like this:

Sorry.

for i=ORIGIN to last(x) - ORIGIN

Valery Ochkov wrote:

Sorry.

for i=ORIGIN to last(x) - ORIGIN

NO! Definitely not. That would be one or more too few, if ORIGIN>0.

But thanks, because you are right, that I made a mistake in this respect ( I should have tried using a value for ORIGIN different to 0).

We can correct it in different way. I guess the way I have chosen in cdec2 is the better one:

As a bonus I added the ability to provide the argument either as a row vector OR as a simple scalar.

Werner

StuartBruff
23-Emerald II
(To:rking-2)

rking-2 wrote:

I need convert binary numbers and show the process, for example:

The binary 1011 is the matrix vector [1 0 1 1], need a program that make 2^3, 0^2, 2^1 and 2^0 then show a matrix [8 0 2 1] and later the sum 11 (8+2+1).

Need show the matrix and sum, working according to the number of digits.

Also need convert (based on block of 3 and 4 digits of) binary number to octal and hexa numbers.

Here's another approach to the conversion from a binary digits vector to a vector of powers and sum - works with any base, not just binary ...

Top Tags