check if number belongs to particular base or not

i am making a number base conversion program in c++,
suppose that user enters an base 2 number and want to convert that number in base 4. when he enter the number (which he wants to be converted), how can i detect that number is belongs to base 2 number system or not.
for example: if user enter 1001011001 , program should continue and convert that number to base 4. and if user enter 1200101 or 1001012 , program should not convert that number and output that this number is not belongs to base 2 number system. waiting for your kind reply. thanks.
The valid digits for base 2 number are {0, 1}.
The valid digits for base 5 number are {0, 1, 2, 3, 4}.
The valid digits for base 42 number are {0, 1, .. ?}.

You could therefore std::find a character from the string with a comparison functor that returns true, if digit is not valid.
can somebody explain it what he said?
Iterate over the number whilst checking if every single char is within the valid range of your operating base.

I think that's what kekiverto meant.
hmmm, i am just new to c++, can you tell me that how can i compare that each digit of a number is within the range?
Well first you can create a string object using:

1
2
int num = 1001010101;
std::string your_number = std::to_string(int);


Now you can "iterate" over the string, since it works like an array, you can use either [int] or .at(int):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool is_base_2 = true;
for (int i = 0; i < your_number.size(); ++i)
{
    if (your_number[i] - '0' >= 2)
    {
        is_base_2 = false;
        break;
    }
}
if (is_base_2)
{
    // ... your conversion code
}
else
{
    // ...
}
how can i detect that number is belongs to base 2 number system or not

Ask the user. There is no way to determine whether a particular string of digits was meant to be a binary representation or a decimal/octal/hex representation if it consists only of digits which are common to all of them just by inspecting it.
Topic archived. No new replies allowed.