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ć?