Binary to Decimal converter

closed account (3872Nwbp)
I have found some other posts about this, but the approach was different from mine. I've gone through my code, it should work from what I can tell... At least the math is right. I've narrowed the problem down to my "void reverse()" function, the "char new_binary_array[20]" variable only contains blank spaces - even though I've set slots to different values. Here is my 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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

void array_reverse();		// Function to reverse the array to correct formatting

char binary_array[20];		// Input array
char new_binary_array[20];	// Array for math
int char_numbers;			// Length of binary string

int _tmain(int argc, _TCHAR* argv[])
{
	string binary_string;
	int decimal_value = 0;

	cout << "Please enter a binary number: ";		// Promt user for string
	cin >> binary_string;
	cout << endl;

	char_numbers = binary_string.length();				// Get length of string
	char* binary_array = (char*)binary_string.c_str();	// Convert from string to array
	array_reverse();			// Function to reverse the array to correct formatting

	if(new_binary_array[0] == '1')						// First bit
		decimal_value = 1;

	for(int a = 1; a < char_numbers; a++)				// Loop for rest of the bits
		if(new_binary_array[a] == '1')
			decimal_value = decimal_value + a * 2;

	cout << binary_string << " in binary is " << decimal_value << " in decimal.";
	cout << endl << endl;

	system("pause");
	return 0;
}

void array_reverse()	// Function to reverse the array to correct formatting
{
	for(int a = 0; a < char_numbers; a++)
		new_binary_array[a] = binary_array[char_numbers - 1 - a];
}


This is for a basic C++ class in high school.
closed account (3872Nwbp)
I'll need to learn some more about vectors and whatever "template" is, I haven't even heard that term. Haha. My class hasn't even gone over classes yet, I only know a little of that, just an example of how basic and slow this class is.
But thank you! It did help.
closed account (D80DSL3A)
The binary_array used in your array_reverse() function is not the char* from line 23, it is the uninitialized character array declared on line 9.
Your reverse_array() is simply copying the contents of one uninitialized array to another.

If you change line 9 to char* binary_array; and line 23 to
binary_array = (char*)binary_string.c_str(); // Convert from string to array
you should find that your code works, although still not as intended.
You also have a problem with how decimal_value is being calculated.
Hint: The a*2 part isn't right.

I had to try your code out because I didn't know if reading the contents of a string through a char* would work. It does! Very cheesy method though. Why not work with strings throughout the program?
closed account (3872Nwbp)
Thank you, that helped a lot. And I was following the hints my teacher gave me... I have found him to be using weird and incorrect methods before though... The internet is usually more helpful then he is. Haha. I had no idea you could just use strings, from what I've been taught, it wouldn't work. But thanks for the clarification.

And the a*2 doesn't work? I had gone through it a few times trying to find the error... I'll look at it. Thanks.

Edit:
I just built it and ran - the formula I had (a*2) works.
Last edited on
closed account (D80DSL3A)
Glad that helped. Make sure you test your program for a few cases.
I tested yours with input = 10100. This should = 4+16 = 20. Your program gives 4+8 = 12.
closed account (3872Nwbp)
Ya I was doing that. And ya, I realized that after. I think I may have fixed the problem... Thanks for the help.

Edit:
I realized what you meant as the math error, I changed the a * 2 to pow(2, a). Works great now. Thanks.
Last edited on
As you are probably aware binary format is easy to convert to hex by selecting the binary digits in groups of four and assigning the hex digit to each group
e.g 1001 1110 1000 = Ox9d8
Hex format is then quite easy to convert to decimal using the dec manipulator in <iomanip>
Or you can just do Total += Digit * 2 ^ CurrentDigit for each digit...
@LB.....nope a little more complex than that.
Topic archived. No new replies allowed.