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.
#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.
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.