Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,892 questions

51,823 answers

573 users

How to convert binary code to text in C++

1 Answer

0 votes
#include <iostream>
#include <string>
#include <bitset>
#include <vector>

std::string bin2text(const std::string& bin_txt) {
    std::string text = "";
    std::vector<std::string> chars;

    // Split binary string into chunks of 8 bits
    for (size_t i = 0; i < bin_txt.length(); i += 8) {
        chars.push_back(bin_txt.substr(i, 8));
    }

    // Convert each binary chunk to a character
    for (const auto& binary : chars) {
        char character = static_cast<char>(std::bitset<8>(binary).to_ulong());
        text += character;
    }

    return text;
}

int main() {
    std::string bin_txt = "0101000001110010011011110110011101110010011000010110110101101101011010010110111001100111";
    
    std::string text = bin2text(bin_txt);

    std::cout << text << std::endl;
}




/*
run:

Programming

*/

 



answered Apr 14, 2025 by avibootz

Related questions

2 answers 248 views
1 answer 76 views
1 answer 66 views
66 views asked Nov 17, 2024 by avibootz
1 answer 67 views
1 answer 80 views
80 views asked Nov 17, 2024 by avibootz
...