Binary to Decimal conversion

Hello,

I was trying to create a program that converts binary int into decimal, but i had some troubles and i can't figure out why.

This is the code:

int twentytwo= 10110;
void setup() {
Serial.begin(9600);
}

void loop() {
int res=0; //partial result
int i=0;
int temp= twentytwo;
while(temp !=0) {
int temp2= pow(2,i); //2^(i)
temp2= temp2*(temp% 2); //2^(i) * 0 or 1
res= res+ temp2; //partial result
Serial.println((String)"res: "+res); //check
i++;
temp= temp/10;
}
Serial.println(" ");
Serial.println(res);
while(1>0) {}
}

The code basically sums 2^i-th digit.
The pow function returns: (1), 2, 4, (8), 16 which is correct (2+4+16= 22)
The point is that the partial sums are incorrect; temp2 is: 0, 2, 3 (intead of 4), 0, 15 (instead of 16).
Why do i get this odd behaviour

int temp2= 1 << i; // NOT pow(2,i);
int twentytwo= 0b10110;

Thanks! But i noticed that i can't convert numbers that are too big (46 -> -12).
The arduino reference says that it can't shift more than 5 (32).
What can i do in that case?

The arduino reference says that it can't shift more than 5 (32).

I don't understand that sentence.
1 << 6 is 64.

Duplicate topics merged

Why did you start a second one ?

TheMemberFormerlyKnownAsAWOL:
I don't understand that sentence.
1 << 6 is 64.

My bad.
But still 46 (101110) is converted to -12.

Sorry for the duplicate post, I didn't mean to, I don't really know what happened.

But still 46 (101110)

sp. "But still 46 (0b101110) "

rygel_b:
My bad.
But still 46 (101110) is converted to -12.

If you want to see that this binary bit patter: 101110 (after conversion) should show this image: 46 on the Serial Monitor, then you may try the following sketch.

void setup() 
{
  Serial.begin(9600);
  byte x = 0b101110;
  byte myDigit[5];
  int i=0;
  
  do
  {
    myDigit[i] = x%10;
    x = x/10;
   // Serial.print(myDigit[i]);
    i++;
  }
  while (x != 0);
  byte decNumber = (myDigit[1]<<4)| myDigit[0];
  Serial.println(decNumber, HEX);  //shows: 46
}

void loop() 
{
  
}

GolamMostafa:
If you want to see that this binary bit patter: 101110 (after conversion) should show this image: 46 on the Serial Monitor, then you may try the following sketch.

void setup() 

{
Serial.begin(9600);
byte x = 0b101110;
byte myDigit[5];
int i=0;

do
{
myDigit[i] = x%10;
x = x/10;
// Serial.print(myDigit[i]);
i++;
}
while (x != 0);
byte decNumber = (myDigit[1]<<4)| myDigit[0];
Serial.println(decNumber, HEX); //shows: 46
}

void loop()
{

}




![smbc.png|630x502](upload://bFERMp8ctbhf7wEnNAVd5Gl8FJe.png)

TL;DR

void setup() 
{
  Serial.begin(9600);
  byte x = 0b101110;
  Serial.println ((int) x, HEX);
}

void loop() 
{}

GolamMostafa:
If you want to see that this binary bit patter: 101110 (after conversion) should show this image: 46 on the Serial Monitor, then you may try the following sketch.

void setup() 

{
  Serial.begin(9600);
  byte x = 0b101110;
  byte myDigit[5];
  int i=0;
 
  do
  {
    myDigit[i] = x%10;
    x = x/10;
  // Serial.print(myDigit[i]);
    i++;
  }
  while (x != 0);
  byte decNumber = (myDigit[1]<<4)| myDigit[0];
  Serial.println(decNumber, HEX);  //shows: 46
}

void loop()
{
 
}




![smbc.png|630x502](upload://bFERMp8ctbhf7wEnNAVd5Gl8FJe.png)

This is nice, but I can't store it in a variable, can I?

What is the "it" you think you can't store?

rygel_b:
Hello,

I was trying to create a program that converts binary int into decimal, but i had some troubles and i can't figure out why.

It’s because your confused. “Integers” don’t have a “format”, they’re just a number. A number is a sequence of one or more bytes in the computers memory.

Format (hex, decimal, binary, octal) are things that are applied to an integer when it is converted to ASCII (or some other character encoding) for display.

It’s therefore impossible to “convert” an integer from binary to an integer in hex, or indeed any other format.

You can convert an integer (note just an integer, no format) into a string in binary/decimal/hex format.
You can convert a string in binary/decimal/hex into an integer, at which point it is just an integer (no format) again.

When you write a program you necessarily have to enter integers as strings (because any text file like an Arduino sketch is nothing more than one big long string at the end of the day), and then the compiler does the work converting that string representation from ASCII to an integer when you compile. Therefore weather you write x = 46, x = 0b101110 or x = 0x2E makes not one bit of difference to the code generated. The compiler just outputs an integer with the required value and devoid of any “format” whatsoever (because it’s now an integer, not a string).

rygel_b:
This is nice, but I can't store it in a variable, can I?

The image 46 is already in the variable named decNumber (Post#7) in this pattern: 0100 0110 (BCD?).

rygel_b:
I was trying to create a program that converts binary int into decimal, but i had some troubles and i can't figure out why.

Ok, firstly, as others said, it seems you have a bit of confusion about number formats, strings, etc. So, if you write:

int twentytwo= 10110;

that variabile will store ten thousands, one hundred and ten, not the binary number 10110!
Secondly, I suppose you mean "a program that converts a binary characters string representing an unsigned positive integer into decimal", thus made of characters "0" and "1".
If this is the goal, is the correct variable to be converted could be:
char twentytwo[] = "10110";

Now, for the conversion, you just need a function to take a string and return an unsigned integer.
Pretty easy and straightforward:

unsigned int bin2uint(char *str) {
  unsigned int val = 0;
  unsigned int exp = 0;
  for(int i=strlen(str)-1 ; i>=0 ; i--)
    val +=((str[i]-'0') << exp++);
  return val;
}