C program to convert octal to binary number system..

i am a beginner and want to learn programs of converting number systems.
now i want to convert octal to binary no. system.
Do you at least have an attempt at the code? If you do then post it.
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
int *dec_binary(int i)
{

	int n = 0;
	int k = 1;
	int *p = NULL;
	while (i / 2) {
		k++;
		p  = (int *)realloc(p, sizeof(int) * k);
		p[n++] = i % 2;
		i /= 2;
	}

	p[n++] = i;
	p[n] = 9;


	for (int k  = 0; k < n / 2; k++) {
		int temp  = p[k];
		p[k] = p[n - k -1];
		p[n -k -1] = temp;
	}

	return p;
		
}
Last edited on
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
int dec_to_oct(int i)
{
	int result = 0;
	int n = 0;
	int first_number = 0;

	if (i < 8)
		return i;

	while (i / 8){
		first_number = i % 8;
		i /= 8;
		n++;
		if (n > 1){
			int temp = 0;
			temp = n - 1;
			while (temp--)
				first_number *= 10;
			first_number += result;
		}
		else
			result = first_number;
		result = first_number;
	}
	while (n--)
		i *= 10;

	i += first_number;

	return i;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int oct_dec(int i)
{
	int result = 0;
	int n = 0;
	int number = 0;

	if (i < 10)
		return i;

	while (i / 10) {
		number = i % 10;
		result += (number * powl(8, n));
		n++;
		i /= 10;
	}
	result += (i * powl(8, n));

	return result;
}
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
int bin_dec(int number)
{


	int result = 0;
	int n = 0;

	int p[10];
	int k = 0;

	while(number / 10) {
		p[k++] = number % 10;
		number /= 10;
	}
	p[k++] = number; 
	p[k] = '\n';
	int j = 0;


	for (int i  = 0; i < k / 2; i++) {
		int temp  = p[i];
		p[i] = p[k - i -1];
		p[k -i -1] = temp;
	}

	int l = 0;
	while(p[l] != '\n')
		cout << p[l++];
	cout << endl;

	k--;
	while (p[n] != '\n') {
		result += (p[n] * powl(2, k));
		n++;
		k--;
	}
	return result;
}
Last edited on
Topic archived. No new replies allowed.