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,894 questions

51,825 answers

573 users

How to convert binary code to text in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *bin2text(const char *bin_txt) {
    int len = strlen(bin_txt) / 8;  // Calculate the number of characters in the binary string
    char *text = (char *)malloc((len + 1) * sizeof(char)); // Allocate memory for the text
    text[len] = '\0'; // Null-terminate the string
    
    for (int i = 0; i < len; i++) {
        char binary[9]; // Temporary array to hold an 8-bit binary chunk
        strncpy(binary, bin_txt + i * 8, 8); // Copy 8 bits from the binary string
        binary[8] = '\0'; // Null-terminate the binary chunk

        // Convert the binary chunk to a decimal value and then to a character
        text[i] = strtol(binary, NULL, 2);
    }
    
    return text;
}

int main() {
    const char *bin_txt = "0101000001110010011011110110011101110010011000010110110101101101011010010110111001100111";
    char *text = bin2text(bin_txt);

    printf("%s\n", text);

    free(text);

    return 0;
}



/*
run:

Programming

*/

 



answered Apr 14, 2025 by avibootz

Related questions

1 answer 75 views
1 answer 160 views
1 answer 80 views
80 views asked Nov 17, 2024 by avibootz
1 answer 70 views
1 answer 67 views
67 views asked Nov 17, 2024 by avibootz
1 answer 76 views
...