Problems parsing binary.

I'm getting a memory access violation with my binary parsing program. Does anyone know what I'm doing wrong?

main.cpp
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
/**
* Project - Binary Parser
* Author: packetpirate
* Last Updated - 03/27/2011
**/

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;
#include "binary.h"
using binary::grab;
using binary::base_10;

void pause();

int main(int argc, char** argv)
{
	cout << "Welcome to Darin's Binary Parser v1.0" << endl;
	cout << "-------------------------------------" << endl;
	string binary = grab<string> ( "Enter a Binary String: " );
	char * binary_ch = (char*)binary.c_str();

	cout << endl;
	cout << "Base 2: " << binary << endl;
	cout << endl;
	cout << "Base 10: " << base_10(binary_ch) << endl;

	pause();
	return 0;
}

void pause()
{
	cin.sync();
	cout << "Press any key to continue...";
	cout.flush();
	cin.get();
}


binary.h
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
/**
* File - binary.h
* Usage - Namespace containing functions to parse and translate binary.
* Last Updated - 03/27/2011
**/

namespace binary
{
	template< typename T >
	T grab(const char * prompt)
	{
		T thing;
		std::istream::iostate old_state = cin.exceptions();
		cin.exceptions(std::istream::failbit);

		while(true)
		{
			try
			{
				cout << prompt;
				cout.flush();
				cin >> thing;
				cin.exceptions(old_state);

				return thing;
			} catch (std::istream::failure &e) {
				cout << "Input failure. Please try again." << endl;
				cin.clear();
				cin.ignore(1024, '\n');
			}
		}
		return thing;
	}

	string base_10(char * my_str)
	{
		char pos = my_str[0];
		int a = 0;
		int b = 7;

		char ch[8];
		char * chp;
		int dec_value = 0;

		string dec_string = "";

		do
		{
			chp = ch;

			for(int i = a;i <= b;i++)
			{
				*chp = my_str[i];
				chp++;
			}

			for(int i = 0;i < 8;i++)
			{
				switch(i)
				{
					case 0:
						if(my_str[i] == '1')
						{
							dec_value += 128;
						}
						else
						{
							dec_value += 0;
						}
						break;
					case 1:
						if(my_str[i] == '1')
						{
							dec_value += 64;
						}
						else
						{
							dec_value += 0;
						}
						break;
					case 2:
						if(my_str[i] == '1')
						{
							dec_value += 32;
						}
						else
						{
							dec_value += 0;
						}
						break;
					case 3:
						if(my_str[i] == '1')
						{
							dec_value += 16;
						}
						else
						{
							dec_value += 0;
						}
						break;
					case 4:
						if(my_str[i] == '1')
						{
							dec_value += 8;
						}
						else
						{
							dec_value += 0;
						}
						break;
					case 5:
						if(my_str[i] == '1')
						{
							dec_value += 4;
						}
						else
						{
							dec_value += 0;
						}
						break;
					case 6:
						if(my_str[i] == '1')
						{
							dec_value += 2;
						}
						else
						{
							dec_value += 0;
						}
						break;
					case 7:
						if(my_str[i] == '1')
						{
							dec_value += 1;
						}
						else
						{
							dec_value += 0;
						}
						break;
					default:
						dec_value += 0;
						break;
				}
			}

			dec_string += "" + dec_value;

			dec_value = 0;

			a += 8;
			b += 8;
		} while(pos != '\0');

		delete chp;
		
		return dec_string;
	}
}
Last edited on
Well for one, you're using a switch case.... You can make a recursive function do this with ease, and it won't be as limiting.

I'll take a look at your specific code if no one else answers your question =)
Last edited on
Everyone can use Google, ultifinitus...

Also, can you give me an example of a recursive function? This was just something I threw together without planning.
Sure, no problem. I just whipped up this piece of junk for ya.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string forward_del(string &tdl, int start = 1){
	string ret = "";
	for (int temp = start; temp < tdl.size(); temp++)
		ret += tdl[temp];
	return ret;
}

int bin_to_int(string binary){
	if (binary.size() == 0)
		return 0;
	if (binary[0] == '0')
		return bin_to_int(forward_del(binary));
	int retval = 1;
	for (int temp = 1; temp < binary.size(); temp++)
		retval *= 2;
	    return retval + bin_to_int(forward_del(binary));
}


sorry for the poor quality, but it does convert binary strings to int values.
Last edited on
I'm not trying to convert it to an INT value. I'm trying to convert the binary string to base 10 and ASCII.
I'm probably the last person that should be giving advice here (I am very new to C++) but why not do it in a loop. I played around with this. Im sure theres a ton of improvements (like a better way to convert a "1" to a 1.

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
#include<iostream>
#include "B2D.h"
#include <math.h>

int main(void)
{
	char a[]={"1111"};
	int d = 0;
	int n=4;
	d = B2D(n,a);
}

int B2D(int n, char Bval[])
{
	int ans=0;
	int dig=0;

		for(int i=0;i<n;i++)
		{
			dig = 0;
			if(Bval[n-i-1]=='1') dig=1;
			ans += dig*pow(2,static_cast<double>(i));

			
		}
return 0;
}
For the second time... I am NOT trying convert the string to an integer! I'm trying to break apart a binary string and convert it to base 10 and ASCII...

Also, so far, nobody has addressed the problem I initially asked for help with, which is the memory access violation.
Last edited on
Line 147 in binary.h is a good candidate to crash. What you are actually doing is adding an integer to the pointer to "". After that it points to an invalid memory address.

But there're other candidates like 'my_str'. You assume that it has at least 8 digits. There's no reason why.

Also on line 24 in main.cpp: Why do you turn the comfortable std::string into an inconvenient c string pointer? casting away the const?

Furthermore in binary.h: The variable 'pos' is never changed. So either you loop once or forever.
Uhhhh the int value... is base 10. and use (char) to convert it to ascii.
I'm trying to break apart a binary string and convert it to base 10 and ASCII...


If i understand you correctly, use a for loop to step through the string and convert every 8 bits.
1
2
3
4
5
6
7
8
9
10
11
12
string bin_to_string(string binary){
	string end = "";
	string buf = "";
	for(int temp = 0; temp <= binary.size(); temp++){
		buf += binary[temp];
		if (buf.size() == 8){
			end += (char)bin_to_int(buf);
			buf = "";	
		}
	}
	return end;
}
Topic archived. No new replies allowed.