Decimal to binary converter

Yes, the student forgot to "preheat the oven".

Sorry, did not see your answers in time ...
Thanks for you help anyway :+1:

Funny though, how I predicted it in reply #23...

Hmmmm, I'm suspicious you're the @louis_f teacher.

RV mineirin

Is anything wrong with atoi()?

Yes, if you use that, you don't see any demonstration of how number bases are converted. This is a learning exercise.
Also, it's deprecated. :slight_smile:

+karma.

Decimal to Binary Conversion:
Let us try to convert 3 decimal number into binary number. Let us keep the decimal number (3) in Busket-1 and the (initial) binary number (0) in Busket-2.

How can we do the decimal to binary conversion? We can follow incrementing and decrementing procedure. In this method, let us increment (increase one-by-one) the contentt of Busket-2 (the binary number) and decrement the contentt of Busket-1 (the decimal number) until Busket-1 is emtied. This is what a human being was following before the advent of mathematics, computer, and programming and even today, we are also following it to give you the fundament concept of converting number from base-10 (decimal) to base-2 (binary). The opposite (binary to decimal) is much more interesting and thrilling!!

1.
Keep initial binary value 0 in Busket-2.
Keep decmal value 3 in Basuket-1.

2.
Increase Busket-2, we get : 1
Decrease Busket-1, we have: 2

Is Busket -1 empty? No!

3.
Increase Busket-2, we get : 10
Decrease Busket-1, we have: 1

Is Busket -1 empty? No!

4.
Increase Busket-2, we get : 11
Decrease Busket-1, we have: 0

Is Busket -1 empty? Yes!

5.
The conversion is complete. Show the Busket-2 to your teacher for the binary bits (11) that you have obtained for the given decimal number (3).

6. How to accomlish the above tasks (Step-1 to Step-5) using Arduino Programming Codes (non-interactive)

Busket-1 ia the variable y to hold decimal number 3. Busket-2 is the variable z to hold binary value. Now, we can have the following declarations and operations:

L1: byte y = 3;          //3 is an image to our eyes
byte z = 0b00000000;//inutial partial binary bits ; 0b says right sided digits are binray bits
L2: z = z+1;    //increase binary value by 1
y = y-1    //decrease decimal value by 1
L3: if(y != 0)
{
    repeat from Label L2;    //goto L2
}
L4: Serial.println(z, BIN);  //conversion complete; show binary bits on Serial Monitor.

7. How to accomlish the above tasks (Step-1 to Step-5) using Arduino Programming Codes (interactive mode using InputBox and OutputBox of Serial Monitor).

(1) We will enter 3 (this is an image) from the InputBox (Fig-1) of Serial Monitor and the binary value (11) will appear on the OutputBox of the Serial Monitor.


Figure-1:

(2) Choose "Line ending tab" of Serial Monitor to "No line ending" and Baud Rate to 9600.

(3) Enter 3 in the InputBox and then click on the Send button. As a result, this bit pattern (00110011) called ASCII Code (Fig-2) travels towards UNO. Once the code has arrived to UNO, catch it and retrive the original image (the 3) and show it on the OutputBox of the Serial Monitor by executing the following codes:

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  byte n = Serial.available();
  if(n != 0)
  {
    char x = Serial.read();
    Serial.println(x);    //shows 3 -- the original image we sent from InputBox of SM
  }
}

(4) ASCII (American Code for Information Intercahnge) Code Table


Figure-2:

8. Now to see (on the Serial Monitor) the binary value 11 for decimal number 3, let us accomplish the following steps:

(1) Let us upload the following sketch in UNO. This program will work for any decimal number from 0 to 9), the bunary result will be from 0 to 1001. You can modify this program to work for any input decimal number from 0 to 255 and even bigger.

byte y;  //y can assume any value form 0 to 9 decimal
byte z = 0b00000000; //intial partial binray value
char x;  //x will hold the ASCII code that is coming from InputBox

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  byte n = Serial.available();
  if (n != 0)
  {
    x = Serial.read();
    Serial.println(x);    //see original image of 3 on Serial Monitor
    //-----------------
    y = x - 0x30; //0x33 - 0x30 = 3  ; retrieving the original image and save as number
    do
    {
      z = z + 1; //increase binary value
      y = y - 1; //decrease decimal value
    }
    while (y !=0);
    Serial.println(z, BIN);  //Serial Monitpr shows: 11
    y = 0;  //reset for next new number (0 to 9)
    z = 0;  //rset for next new number (0to 9)
  }
}

(2) Open Serial Monitor to see the following outputs:

0
0
1
1
2
10
3
11
4
100
5
101
6
110
7
111
8
1000
9
1001
1 Like

So the OP is trying to develop an algorithm that will work like atoi() works (which, by the way, I had no idea was deprecated [by whom?], but I use for myself with no ill consequences to this day).

Alright, but in that case, why is he using an arduino at all instead of programming his computer? Wouldn't that be faster and with less overhead and hardware complications to worry about?

It was so bad, he gave everyone a second chance :joy:
Like last time, dead line tonight at 12.

// C++ code
//
int dividende;
int quotient=9;
 String reste;

void setup()
{
  Serial.begin(9600); // ouvre le port série à 9600 bauds
  dividende=quotient;
  if (quotient<=255)
 {
  while (quotient!=0)
  {
  reste=reste+String(quotient%2);
  quotient=(quotient/2);
  }
  Serial.print(reste);
 }
}

void loop()
{
 
}

Here, i have to print my values on 8 bits and i need a "reverse" function to convert 0001 into 1000 (in the case of 8 as decimal value)
:slight_smile:

So, what is your initial thinking about this? I hope that the fact that you're posting here, does not mean that you have not made some initial investigations and have not thought about a solution?

Hi, i have managed to implement an inverter but i have no clue on how to print it on 8 bits

// C++ code
//
int dividende;
int quotient=15;
int i;
 String reste;
 String inverse;


void setup()
{
  Serial.begin(9600); // ouvre le port série à 9600 bauds
  dividende=quotient;
  if (quotient<=255)
 {
  while (quotient!=0)
  {
  reste=reste+String(quotient%2);
  quotient=(quotient/2);
  }
    String o = reste;
    int length = reste.length();
        for (i = 0; i <= length; i++)
        {
          inverse += reste[length-i];
        }
  Serial.println(inverse);
 }
}

void loop()
{

}

I didn't look closely but the program should print 8 bits. What does it print?

Only binary value of selected decimal value... i mean if i print 5 (101) and 15(1111)
I want it to print "5" ( 0000 0101) or "15" (0000 1111)
if you see what i mean

No, I'm not clear on the assignment. What output? Give an example. Your last post left me more confused than I was before you posted it.

i don't know how to explain it but, whatever the chosen value, I want it to print 8 binary values on the monitor as an output.
Whether it's for 5 or for 15 or any other value :sweat_smile:

But... your program already prints 8 binary values. Do you see my problem understanding? It the assignment to input different values from the keyboard and then convert them.?

In other words, how are your "chosen values" chosen?

Anyway, you have to explain it, otherwise nobody can help you here. Try using a language translator if you're having trouble converting to English...

Yeah, this is less clear by the day. Can you fill up this example?

I have         I want
     5         .....
    15         .....
2 Likes

By Jove, I think I got it! He wants to output a zero-padded string representing the number in binary notation, that will always contain 8 chars. See post #58.

1 Like

They are chosen manually, in the code. it's the: int quotient=5;