Decimal to binary converter

The real fun begins when the students are told, "okay, now do it without using print()". :slight_smile:

Blink Morse on LED_BUILTIN

Hi, @louis_f

To add code please click this link;

Tom... :smiley: :+1: :coffee: :australia:

1 Like

Hello again,
I have modified my code, it gives the right "answer" but i tried to implement a line to print the value before it's conversion ... but it gives a stupid value and not what i entered :smiley:

int incomingByte = 0;

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

void loop()
{
   // envoie une donnée sur le port série seulement quand reçoit une donnée
  if (Serial.available() > 0) 
  {
   // si données disponibles sur le port série
    // lit l'octet entrant
    incomingByte = Serial.read();

    // renvoie l'octet reçu
    Serial.print(incomingByte);
    Serial.print('\t');      // affiche un tab
    Serial.print("Base 2 -> ");
    Serial.println(incomingByte - '0', BIN);
  }
}

In this case, i tried:
5
6
7
8
image
and it returns
53
54
55
56

Any idea ?

The ASCII code for '0' is 48, for '1' it's 49, for '2' it's 50...
See the pattern?

yeah ...
is there a way to get rid of that char equivalent ?

Yes - see my earlier post that you marked as the solution.

That's what i did but it doesn't change anything :thinking:

You only did half the job.

aaaahhh .
I see, it works now, thanks !

image

And now, if you put that code into a function, you only need to do the subtraction once.

Hi @louis_f
You said this was a school assignment.
Which school course are you attending?

RV mineirin

Turns out that the teacher didn't want it this way but with Euclidean division.
have to start all over again and i have to hand it in for tonight :sob:
Anyway, thanks for your help ^^

I doubt that very much. The only thing you need to change is the print method.

What is your time zone?
Do you still have time to redo?
Try it like this:

Serial.println(incomingByte%48,BIN);

%48 is a division by 48 and the printout shows the rest.

RV mineirin

@RV, I believe they've been asked to do the base 2 conversion "the hard way". Your solution would be rejected by the professor.

@aarg
In theory an Euclidean division is an A/B split.
the rest r is part of the Euclidean division.
Therefore A = inputByte
B = 48 (divider).
And the impression is the rest r.

RV mineirin