Help with Base2 to base10

The title says everything. The code doesn't need to be the best, just to use minimum space. Thanks. I'm new to the forum so say if I did anything wrong!

You have to explain with a bit more detail what you need. Where does your base2 data come from? Hpw big can it be?

The usual candidate would be strtoul if your base2 is a text.

It is an Int value, example: 1011

ihavenoideea:
The title says everything. The code doesn't need to be the best, just to use minimum space. Thanks. I'm new to the forum so say if I did anything wrong!

Not sure what you need. All numbers are stored in base 2, it just depends on how you display them. Are you needing to convert some ASCII input into binary?

ihavenoideea:
It is an Int value, example: 1011

Is that a string from the serial port console or what? BTW, that is 11 decimal.

Let me explain.

I have an Base2 number(010) and I would want to transform it into an Base10 number(2)

ihavenoideea:
Let me explain.

I have an Base2 number(010) and I would want to transform it into an Base10 number(2)

You just did. :slight_smile: But seriously, the problem is that you haven't explained where these numbers reside, and in what storage format. For example, are these character strings, i.e. "text"? I looked back and see three attempts to pry that information from you.

It is an Int value, example: 1011

You confuse everyone by capitalizing "Int". Now we're not sure if you really mean a C "int" type, or you are just saying, "integer" in a strange way.

Either way, we still don't know how you want to represent, store or display the decimal result.

It would also be helpful if you explained what your purpose or application is.

Simplest program I can come up with:

void setup() 
{
  Serial.begin(9600);
  int var = 0b1011;
  Serial.println(var);
}

void loop()
{
}

This is an project with no Serial as I use it only for debugging, and as I say, the input should be an normal(short int) value and the result an short int.

Like this:

short int CalculateBase10(short int n){
//DoSomething
return Result;
}

Are you wanting binary coded decimal?

You have not given enough context for anyone to understand what you are trying to do.

ihavenoideea:
This is an project with no Serial as I use it only for debugging, and as I say, the input should be an normal(short int) value and the result an short int.

Like this:

short int CalculateBase10(short int n){
//DoSomething
return Result;
}

Ummmm...OK. Like I said, all numbers are stored as binary. It just depends on how you display them.

Yeah, binnary to decimal

Also, I want to display them as Decimal, with no Serial.

Well, any short int is binary. So you have to explain what format you expect the result to be in. Why not post some example numbers and their results?

ihavenoideea:
Yeah, binnary to decimal

Also, I want to display them as Decimal, with no Serial.

Okay, then it's

short int CalculateBase10(short int n){
return n;
 }

Because for example

Serial.print(CalculateBase10(13));

and

Serial.print(13);

both print "13".

In other words, Serial.print already converts binary to decimal by default.

Also, I want to display them as Decimal, with no Serial.

Display them where?

We're pulling so many teeth here, we will soon need a dental assistant.

I arleady did.

I have an Base2 number(010) and I would want to transform it into an Base10 number(2)

With no automatically transformed String/Serial function.

Also, aarg. I wanted from Binnary to Decimal, not Decimal to Decimal.

Or maybe this will help you understand:

void setup() 
{
  Serial.begin(9600);
  short int var = 11;
  Serial.print("Base 10 = ");
  Serial.println(var, DEC);
  Serial.print("Base 2 = ");
  Serial.println(var, BIN);
}

void loop()
{
}

This prints:

Base 10 = 11
Base 2 = 1011

Computers can not store decimal numbers directly. It is an impossibility.

aarg:
Computers can not store decimal numbers directly. It is an impossibility.

In other words, computers store EVERYTHING in binary. You can display the data in whatever format you want.

I arleady said no Serial. And, aarg. If computers cannot store decimal numbers, why int n = 15 exists? Yes, it is transformed into Binnary, but to transform from Binnary into Decimal is possible. Let me explain what I'm doing.

So, I send some data from an arduino to another one(Lets say it sends 5) but that 5 is encoded to Binnary(Using an function) and send bit by bit. The 2nd arduino receives the binary text(Lets say 1101) and with another function it decodes it into an Decimal value.

Also, I'm saying this because the result

Serial.print(Binarry,DEC)

will be different than

if(Binnary==5) because it just isn't working.

ihavenoideea:
I arleady said no Serial. And, aarg. If computers cannot store decimal numbers, why int n = 15 exists?

'int n = 15' is how you tell the compiler to store the (decimal) number 15. The compiler translates that to instructions to store the number 15. It is in binary in the hardware.