Base converter not working in extreme cases

Hey guys! I have a quick question. I have a code that outputs a snippet of numbers (The display function in line 90 states the StartPoint and EndPoint) and displays every number in between in decimal, binary, and factorial representation.

Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
using namespace std;

int Exponent(int Base, int Exponent)
{
   int ReturnValue = 1;
   while(Exponent != 0)
  {
		ReturnValue = ReturnValue*Base;
		Exponent--;
	}
	return ReturnValue;
}

int BinaryConverter(int BaseTenValue)
{
	int ReturnValue, CounterValue, ModValue, ExponentCounter, NegativeTest, Binary;
	ModValue = 1;
	ExponentCounter = 0;
   ReturnValue = 0;
	while(BaseTenValue % ModValue != BaseTenValue)
	{
		ModValue = ModValue * 2;
		ExponentCounter++;
	}
	ModValue = ModValue/2;
   ExponentCounter--;
	CounterValue = BaseTenValue;
   while(CounterValue > 0)
	{
		NegativeTest = CounterValue - ModValue; 
		if(NegativeTest >= 0)
		{
			Binary = Exponent(10, ExponentCounter);
			CounterValue = CounterValue - ModValue;
			ReturnValue = ReturnValue + Binary;
		}
		ModValue = ModValue/2;
      ExponentCounter--;
   }
	return ReturnValue;
}

int FactorialConverter(int BaseTenValue)
{
   int ReturnValue, CounterValue, ModValue, ExponentCounter, NegativeTest, Binary;
	ModValue = 1;
	ExponentCounter = 0;
   ReturnValue = 0;
	while(BaseTenValue % ModValue != BaseTenValue)
	{
   	ExponentCounter++;
		ModValue = ModValue * ExponentCounter;
	}
	ModValue = ModValue/ExponentCounter;
   ExponentCounter--;
	CounterValue = BaseTenValue;
   while(CounterValue > 0)
	{
		NegativeTest = CounterValue - ModValue; 
		if(NegativeTest >= 0)
		{
			Binary = Exponent(10, ExponentCounter);
         Binary = Binary/10;
			CounterValue = CounterValue - ModValue;
			ReturnValue = ReturnValue + Binary;
		}
      else
      {
		   ModValue = ModValue/ExponentCounter;
         ExponentCounter--;
      }
   }
	return ReturnValue;
}

void Display(int StartPoint, int EndPoint)
{
   int Counter = StartPoint;
   cout << "BASE TEN \t BINARY \t FACTORIAL" << endl;
   while (Counter <= EndPoint)
   {
      cout << Counter << "\t \t" << BinaryConverter(Counter) << "\t \t" << FactorialConverter(Counter) << endl;
      Counter++;
   }
}

int main ()
{
   Display(1, 24);
   return 0;
}


My question is this: The binary converter works perfectly for numbers up until 1024, then it displays a very odd number. Why is this? The factorial converter also works until 3628800 or 10! Do you know why, for both cases, it stopped working at the 10th power/factorial? It's very odd and confusing, and I've been thinking about it for a while.

Also, if you weren't aware, here's a snippet of a definition of factorial representation:
a string of numbers is to be interpreted in factorial base: the i-th number from the right ranges from 0 to i and tells you what multiple of i! to add. For example,
20301! = 2·5! + 0·4! + 3·3! + 0·2! + 1·1! = 240 + 18 + 1 = 259.

Thanks for the support!
Because 4-bytes int can only hold vaues from -2147483647 to 2147483647
Ohkay. Is there a way to make it larger?
use long long, but it is a temporary solution.
Instead of using integral types, use std::string to store digit string.
How would I go about doing that? I've never used a digit string
1
2
3
4
5
std::string x("101");//Hi! I am 5 n binary
//or
std::string y;
y += '1';
y += "01";//Now I am too! 
*facepalm* Thank you MiiNi....I was having a major brainfart. I'll try that next. by the way, what did you think about the code? Anything you would have done differently?
When it comes to converting an integer to a binary string, there are lots of ways to go about it.
Here's one example:
1
2
3
4
5
6
7
8
9
#include <bitset>
#include <string>

std::string toBinary(int num)
{
    const int len = 8 * sizeof(int);
    std::bitset<len> bits(num);
    return  bits.to_string();
}


Other ways include masking individual bits of the number and using bitwise operations. For example in this thread: http://www.cplusplus.com/forum/beginner/97469/#msg523129

Of course this works because the integer type is represented internally in binary. The idea that the number is converted from decimal to binary is a misconception. It is actually converted from the computer's internal binary representation into a human-readable binary representation.
Topic archived. No new replies allowed.