dec to binary failed...pls help

I want to do a conversion tool from decimal number to binary. I tried calculated on paper many times according to the steps in the code which I believe its logical and smooth. However it outputs "0"digits in front and "1" at last for every execution and I dun know why...

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
void dectobin(){
    cout<<"Please input the decimal number to be converted into binary:\n";
    int number;
    cin>> number;

    int nofbits=1;  
    while (number>1){      //count the number of bits to use
        number=number/2;
        ++nofbits;
    }
    int x= number;
    int bits;
    
    while (nofbits!=0)
    {
        for (int i=0; i<nofbits-2; i++)
        {
            x=(x/2);
        }
        bits=x%2;
        cout<<bits;
        nofbits--;
        x=number;
        cout<<"x:"<<number<<"\t"<<x<<"\n";
    }
    
    cout<<"B"<<endl;

}

int main ()
{
dectobin();
return 0;
}
Last edited on
closed account (SECMoG1T)
Hi i got a number of functions that i used some months ago for conversions maybe they might help you but note that some were done carelessly so you might need ignore some features.


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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#ifndef _FUNCTIONS_H
#define _FUNCTIONS_H

#include <string>
#include <vector>
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <numeric>
#include <cmath>
#include <map>


unsigned long long m_pow(unsigned long long left, unsigned long long right)
{
		unsigned long long multi = left;
		right -= 1;
		
		while (right>0)
		{
			left *= multi; --right;
		}
		return left;
}

void format_bin_mode(std::string& s, unsigned long long digits=4)
{
	auto size = s.size();

	if (size%digits != 0)
	{
		auto to_add = (digits - (size%digits));
		s.insert(0, to_add, '0');
		size += to_add;
	}

	if (size>digits)
	{
		unsigned long long counter = 0;
		std::string temp{};
		while (counter < size-1)
		{
			temp += s.substr(counter, digits)+" ";
			counter += digits;
		}
		s = temp;
	}
}


std::string  dec_to_bin(unsigned long long no)
{
	std::string data{};
	if (no>1)
	while (no)
	{
		data += std::to_string((no % 2));
		no /= 2;
	}

	else 
		data += std::to_string((no));
	std::reverse(data.begin(), data.end());
	format_bin_mode(data);
	return data;
}

unsigned long long bin_to_dec(const std::string& s)
{
	unsigned long long size = s.size();
	std::vector<unsigned long long> accum;

	for (unsigned long long x = 0; x<size; x++)
	{
		if (s[x] == '1')
		{
			unsigned long long loc = ((size - 1) - x);
			accum.push_back(pow(2, loc));
		}
	}
	return std::accumulate(accum.cbegin(), accum.cend(), 0ull);
}

std::string bin_to_hex(std::string& s)
{
	const std::vector<std::string> hex{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
	std::remove_if(s.begin(), s.end(), [](char&a){return a == ' '; });

	std::string data{};

	unsigned long long i = 0;
	while (i < (s.size() - 1))
	{
		auto pos = bin_to_dec(s.substr(i, 4));
		data += hex[pos];
		i += 4;
	}

	data.insert(0, "0x");
	return data;
}

std::string hex_to_bin(std::string s)
{
	std::string data{};
	
	if (s.find_first_not_of(std::string("xX0123456789ABCDEF")) != std::string::npos)
		throw std::invalid_argument("invalid data\n");

	std::map<char, std::string> lib
	{ { '0', "0000"}, { '1', "0001"}, { '2', "0010"}, { '3', "0011"}, { '4', "0100"}, 
	  { '5', "0101"}, { '6', "0110"}, { '7', "0111"}, { '8', "1000"}, { '9', "1001"},
	  { 'A', "1010"}, { 'B', "1011"}, { 'C', "1100"}, { 'D', "1101"}, { 'E', "1110"}, { 'F',"1111"}
	};

	if (s.find("0x") != std::string::npos)
		s.erase(s.begin(), s.begin()+2);

	for (char & c : s)
	{
	  data+=lib[c];
	}

	format_bin_mode(data);
	return data;
} 

std::string dec_to_hex(unsigned long long dci)
{
	auto data = dec_to_bin(dci);
	auto hex  = bin_to_hex(data);
	return hex;
}


unsigned long long oct_to_dec(unsigned long long oct)
{
	const std::string data = std::to_string(oct);
	std::size_t size = data.size() - 1;
	std::map<char,std::size_t> lib
	{ { '0', 0 }, { '1', 1 }, { '2', 2 }, { '3', 3 }, { '4', 4 }, 
	  { '5', 5 }, { '6', 6 }, { '7', 7 }, {'8',8}
	};

	std::size_t loc = 0;
	unsigned long long resl = 0;
	
	while (loc <= size)
	{
		int dif = (size - loc);
		resl += (lib[data[loc]]*pow(8,dif));
		++loc;
	}

	return resl;
}

std::size_t dec_to_oct(std::size_t dec)
{
	std::string data{};

	if (dec < 8 && dec >= 0)
		return dec;

	while (true)
	{
		if (dec < 8)
		{
			data += std::to_string(dec);
			break;
		}
		data += std::to_string(dec % 8);
		dec /= 8;
 	}
	
	std::reverse(data.begin(), data.end());
	return std::stoull(data);
}

std::string oct_to_bin(std::size_t oct)
{
	return dec_to_bin(oct);
}

std::string oct_to_hex(std::size_t oct)
{
	auto bin = oct_to_bin(oct);
	return bin_to_hex(bin);
}

#endif ///_FUNCTIONS_H 
Topic archived. No new replies allowed.