Converting DEC to BCD

I have to convert a decimal number (1-255) to packed binary-coded decimal.

I have the idea of how it's supposed to work.

Example:
Convert 133 to BCD
1. Separate 133 to individual numbers (1, 3, 3).
2. Convert each number to binary (0001, 0011, 0011).
3. Print it in the form of "133 - 0001 0011 0011".

However, I'm struggling how to get the number separation and whatnot.
I'm just really confused how to program this.

Also, the decimal numbers have to be in 3 values, (eg; 4 has to be 004)
Because the result to BCD should be 0000 0000 0100, rather than just 0100.

If someone can help me with some C++ algorithm or snippet of code with some explanation, I would really appreciate it.
Last edited on
You can get last number from bigger number by %-operator. For example like this:
1
2
123 % 10 // =3
179 % 10 // =9 


After that you can handle "separated" number how you want ;) And how about next number?
(current_number-remainder)/10
Topic archived. No new replies allowed.