Skip to main content
I think that characters '0' and '1' were meant (otherwise there would be no difference)
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327

I would update Emily L's answer in the following ways:

  • return values should be returned, not using "out" parameters.
  • It is not "decimal to binary". It is an integer in internal form, which is in no way "decimal".
  • use uint8_t, or if using a char actually store 0'0' and 1'1' rather than 0 and 1.
  • prefer prefix increment.

You don't need to explain to us how formatting a number in some base works. Though, your explanation does help us know your level of understanding at this point.

To clarify the second point above: you are not "converting" a number to binary. You are formatting a number into a binary representation. Normally this is done to produce a string, not an array of integers. The native number is an abstraction, not a text string in any base. Though, you can be aware that the internal representation in the CPU is in fact binary: you are doing this with the use of >> instead of /2, though you are staying abstract with %2.

It might be instructive to have your function format a number as any base. Then you will stick to the abstraction of the native number as "an integer" not a group of bits, and it provides a rationale as to why you return an array of small numbers rather than a string: another function will assign character representation to the digits, taking care of what to do with bases > 10, or Chinese digits etc.

The function would look like this:

template <typename T>
std::vector<uint8_t> express_as_base (T number, uint8_t base)
{
    std::vector<uint8_t> result;
    if (number < 0) {
       number= -number;
       result.push_back(255);  // special flag indicates negative
    }
    while (number) {
       const auto digit= number % base;
       number /= base;
       result.push_back (digit);
    }
    return result;
}

More note on your original code:
return(0);
Don't put extra parens around your return value. return is not a function call! Besides being idiomatic (different things should look different), habitually using these extra parens can actually mean something different as of C++11. It will suppress automatically using move when returning a suitable local value, and for some kinds of templates, changes the deduced value category for the return type.

You never use anything from <stdio.h>. And if you were, you should be using <cstdio> instead. And you should really be using the C++ library functions, not the old C library.

I would update Emily L's answer in the following ways:

  • return values should be returned, not using "out" parameters.
  • It is not "decimal to binary". It is an integer in internal form, which is in no way "decimal".
  • use uint8_t, or if using a char actually store 0 and 1 rather than 0 and 1.
  • prefer prefix increment.

You don't need to explain to us how formatting a number in some base works. Though, your explanation does help us know your level of understanding at this point.

To clarify the second point above: you are not "converting" a number to binary. You are formatting a number into a binary representation. Normally this is done to produce a string, not an array of integers. The native number is an abstraction, not a text string in any base. Though, you can be aware that the internal representation in the CPU is in fact binary: you are doing this with the use of >> instead of /2, though you are staying abstract with %2.

It might be instructive to have your function format a number as any base. Then you will stick to the abstraction of the native number as "an integer" not a group of bits, and it provides a rationale as to why you return an array of small numbers rather than a string: another function will assign character representation to the digits, taking care of what to do with bases > 10, or Chinese digits etc.

The function would look like this:

template <typename T>
std::vector<uint8_t> express_as_base (T number, uint8_t base)
{
    std::vector<uint8_t> result;
    if (number < 0) {
       number= -number;
       result.push_back(255);  // special flag indicates negative
    }
    while (number) {
       const auto digit= number % base;
       number /= base;
       result.push_back (digit);
    }
    return result;
}

More note on your original code:
return(0);
Don't put extra parens around your return value. return is not a function call! Besides being idiomatic (different things should look different), habitually using these extra parens can actually mean something different as of C++11. It will suppress automatically using move when returning a suitable local value, and for some kinds of templates, changes the deduced value category for the return type.

You never use anything from <stdio.h>. And if you were, you should be using <cstdio> instead. And you should really be using the C++ library functions, not the old C library.

I would update Emily L's answer in the following ways:

  • return values should be returned, not using "out" parameters.
  • It is not "decimal to binary". It is an integer in internal form, which is in no way "decimal".
  • use uint8_t, or if using a char actually store '0' and '1' rather than 0 and 1.
  • prefer prefix increment.

You don't need to explain to us how formatting a number in some base works. Though, your explanation does help us know your level of understanding at this point.

To clarify the second point above: you are not "converting" a number to binary. You are formatting a number into a binary representation. Normally this is done to produce a string, not an array of integers. The native number is an abstraction, not a text string in any base. Though, you can be aware that the internal representation in the CPU is in fact binary: you are doing this with the use of >> instead of /2, though you are staying abstract with %2.

It might be instructive to have your function format a number as any base. Then you will stick to the abstraction of the native number as "an integer" not a group of bits, and it provides a rationale as to why you return an array of small numbers rather than a string: another function will assign character representation to the digits, taking care of what to do with bases > 10, or Chinese digits etc.

The function would look like this:

template <typename T>
std::vector<uint8_t> express_as_base (T number, uint8_t base)
{
    std::vector<uint8_t> result;
    if (number < 0) {
       number= -number;
       result.push_back(255);  // special flag indicates negative
    }
    while (number) {
       const auto digit= number % base;
       number /= base;
       result.push_back (digit);
    }
    return result;
}

More note on your original code:
return(0);
Don't put extra parens around your return value. return is not a function call! Besides being idiomatic (different things should look different), habitually using these extra parens can actually mean something different as of C++11. It will suppress automatically using move when returning a suitable local value, and for some kinds of templates, changes the deduced value category for the return type.

You never use anything from <stdio.h>. And if you were, you should be using <cstdio> instead. And you should really be using the C++ library functions, not the old C library.

Source Link
JDługosz
  • 11.7k
  • 19
  • 40

I would update Emily L's answer in the following ways:

  • return values should be returned, not using "out" parameters.
  • It is not "decimal to binary". It is an integer in internal form, which is in no way "decimal".
  • use uint8_t, or if using a char actually store 0 and 1 rather than 0 and 1.
  • prefer prefix increment.

You don't need to explain to us how formatting a number in some base works. Though, your explanation does help us know your level of understanding at this point.

To clarify the second point above: you are not "converting" a number to binary. You are formatting a number into a binary representation. Normally this is done to produce a string, not an array of integers. The native number is an abstraction, not a text string in any base. Though, you can be aware that the internal representation in the CPU is in fact binary: you are doing this with the use of >> instead of /2, though you are staying abstract with %2.

It might be instructive to have your function format a number as any base. Then you will stick to the abstraction of the native number as "an integer" not a group of bits, and it provides a rationale as to why you return an array of small numbers rather than a string: another function will assign character representation to the digits, taking care of what to do with bases > 10, or Chinese digits etc.

The function would look like this:

template <typename T>
std::vector<uint8_t> express_as_base (T number, uint8_t base)
{
    std::vector<uint8_t> result;
    if (number < 0) {
       number= -number;
       result.push_back(255);  // special flag indicates negative
    }
    while (number) {
       const auto digit= number % base;
       number /= base;
       result.push_back (digit);
    }
    return result;
}

More note on your original code:
return(0);
Don't put extra parens around your return value. return is not a function call! Besides being idiomatic (different things should look different), habitually using these extra parens can actually mean something different as of C++11. It will suppress automatically using move when returning a suitable local value, and for some kinds of templates, changes the deduced value category for the return type.

You never use anything from <stdio.h>. And if you were, you should be using <cstdio> instead. And you should really be using the C++ library functions, not the old C library.