Function to convert from binary to decimal

Hi all!

Just in case this could be useful: Here you have a function that transforms a byte into an int:

int binary2decimal(byte b)
{

int dec = 0;
int power = 1;
byte mask;
int weight;

for (mask = 0x01; mask; mask <<= 1)
{
if (b & mask)
{
weight = 1;
}
else
{
weight = 0;
}

dec = dec + (power * weight);
power = power * 2;

}

return dec;
}

CHEERS!!

Cristina

Cool. That could be a nice basis for a tutorial on bit manipulation, etc. But if you just want to convert a byte to an int, you can just do this:

byte b = 10;
int dec = b;

or, if you need to pass a byte to a function expecting an integer, just do it:

byte b = 10;
printInteger(b);