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

51,859 answers

573 users

How to convert hexadecimal number to binary in C

1 Answer

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

const char *hex_char_to_binary(char ch) {
    switch (toupper(ch)) {
        case '0': return "0000";
        case '1': return "0001";
        case '2': return "0010";
        case '3': return "0011";
        case '4': return "0100";
        case '5': return "0101";
        case '6': return "0110";
        case '7': return "0111";
        case '8': return "1000";
        case '9': return "1001";
        case 'A': return "1010";
        case 'B': return "1011";
        case 'C': return "1100";
        case 'D': return "1101";
        case 'E': return "1110";
        case 'F': return "1111";
    }
    return "";
}
 
char *hex_to_binary(char *hex, int size, char *binary) {
    for (int i = 0; i != size; i++)
       strcpy(binary + i * 4, hex_char_to_binary(hex[i]));
        
    return binary;
} 
 
int main() {
    char hex[8] = "1BC", binary[32]; 
   
    printf("%s", hex_to_binary(hex, 8, binary)); 
}
 
 
/*
run:
 
000110111100
 
*/

 



answered Apr 1, 2019 by avibootz

Related questions

1 answer 125 views
125 views asked Sep 17, 2021 by avibootz
1 answer 121 views
121 views asked Aug 24, 2021 by avibootz
1 answer 129 views
1 answer 195 views
1 answer 113 views
113 views asked Sep 17, 2021 by avibootz
1 answer 116 views
116 views asked Aug 24, 2021 by avibootz
...