lettura simple

The Scilab base2dec() Function

In the Scilab environment, there's a nifty function known as base2dec(). This function elegantly converts a number from any base 'n' to its decimal equivalent. Here's how it's structured:

base2dec(s,n)

It takes two parameters:

  • 's': This is a string that encapsulates a number in base 'n'.
  • 'n': An integer, denoting the base of the number you're aiming to transform into a decimal.

What you get in return is the number neatly transcribed in decimal notation, or in layman's terms, base 10.

For those looking to convert multiple numbers simultaneously, the function supports an array of strings as input. When you feed it such an array, it diligently processes each string, converting each to its respective decimal value.

Let's delve into a few practical examples to elucidate.

Enter base2dec('101',2)

base2dec('101',2)

Here, '101' stands for a binary value (base 2). In the decimal world, that's a 5.

So, the output unfurls as:

ans=
5.

Now, let’s try another. Input base2dec('101',8)

base2dec('101',8)

In this scenario, '101' is an octal representation (base 8).

The decimal counterpart? A crisp 65.

ans=
65

Venturing into hexadecimal, type in base2dec('101',16)

base2dec('101',16)

With '101' as a hexadecimal (base 16), it translates to the decimal 257.

ans=
257

For those with an array of values, feed them all in one go.

For example, base2dec(['10','11','12'],4)

base2dec(['10','11','12'],4)

This trio - '10', '11', and '12' - are numbers in base 4.

Their decimal avatars? 4, 5, and 6, respectively.

ans=
4, 5, 6

This last example showcases that with the base2dec() function, you're not limited to just binary, octal, or hexadecimal bases – you can specify any starting base you'd like.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin