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 text to binary code in C

1 Answer

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

void text2bin(const char *txt, char *bin) {
    int len = strlen(txt);
    bin[0] = '\0'; // Initialize the bin string
    
    char temp[1024] = "";
    strcpy(temp, txt);

    for (int i = 0; i < len; i++) {
        char binary[9] = {0}; // 8 bits for binary representation + null terminator
        for (int j = 7; j >= 0; j--) {
            binary[j] = (temp[i] & 1) ? '1' : '0';
            temp[i] >>= 1;
        }
        strcat(strcat(bin, binary), " ");
    }
}

int main() {
    const char *txt = "C Programming";
    char bin[256] = {0}; 

    text2bin(txt, bin);
    
    printf("%s\n", bin);

    return 0;
}



/*
run:

01000011 00100000 01010000 01110010 01101111 01100111 01110010 01100001 01101101 01101101 01101001 01101110 01100111 

*/

 



answered Nov 17, 2024 by avibootz

Related questions

1 answer 68 views
1 answer 70 views
1 answer 67 views
67 views asked Nov 17, 2024 by avibootz
1 answer 76 views
1 answer 70 views
1 answer 73 views
1 answer 75 views
...