Binary To String. Niepoprawne działanie funkcji

0

Mam oto taki kod:

#include <iostream>
#include <bitset>

using namespace std;

string DecToBin(int i)
{
	return bitset<8>(i).to_string();
}

int BinToDec(string s)
{
	return bitset<8>(s).to_ulong();
}

int CharToDec(char c)
{
	return static_cast<int>(c);
}

char DecToChar(int i)
{
	return static_cast<char>(i);
}

string CharToBin(char c)
{
	char c_ = CharToDec(c);
	return DecToBin(c_);
}

char BinToChar(string s)
{
	char s_ = BinToDec(s);
	return DecToChar(s_);
}

void StrToBin(string s)
{
	for(int i=0; i< s.length(); i++)
	{
		string x = CharToBin(s[i]);	
		cout << x << " ";
	}
}

void BinToStr(string s)
{
	
	for(int i=0; i< s.length()/8; i++)
	{
	  char r = BinToChar(s);
		
		cout << r << " ";
	}

}

int main()
{
	BinToStr("01100010 01100010 01100001");
}

Program powinien wypisać : "b b a", a zamiast tego wypisuje "b b b".
Czy ktoś wie jak to poprawić?

0
for(int i=0; i< s.length()/8; i++)
{
    char r = BinToChar(s);
    cout << r << " ";
}

Jak może wyświetlić coś innego skoro za każdym razem dostaje ten sam argument?

0

I po sprawie:

void BinToStr(string s)
{
	istringstream buffer(s);
	string str;
	
	while( buffer >> str)
	{
		cout << BinToChar(str);
	}
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1