Need suggestion with Hexadecimal to Binary converter.

This is a discussion on Need suggestion with Hexadecimal to Binary converter. within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h>#define MAX 1000 int main(){ char binaryNumber[MAX],hexaDecimal[MAX]; long int i=0; printf("Enter any hexadecimal number: "); scanf("%s",hexaDecimal); printf("\nEquivalent binary value: ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    Need suggestion with Hexadecimal to Binary converter.

    Code:
    #include<stdio.h>#define MAX 1000
    
    
    int main(){
        char binaryNumber[MAX],hexaDecimal[MAX];
        long int i=0;
    
    
        printf("Enter any hexadecimal number: ");
        scanf("%s",hexaDecimal);
    
    
        printf("\nEquivalent binary value: ");
        while(hexaDecimal[i]){
             switch(hexaDecimal[i]){
                 case '0': printf("0000"); break;
                 case '1': printf("0001"); break;
                 case '2': printf("0010"); break;
                 case '3': printf("0011"); break;
                 case '4': printf("0100"); break;
                 case '5': printf("0101"); break;
                 case '6': printf("0110"); break;
                 case '7': printf("0111"); break;
                 case '8': printf("1000"); break;
                 case '9': printf("1001"); break;
                 case 'A': printf("1010"); break;
                 case 'B': printf("1011"); break;
                 case 'C': printf("1100"); break;
                 case 'D': printf("1101"); break;
                 case 'E': printf("1110"); break;
                 case 'F': printf("1111"); break;
                 case 'a': printf("1010"); break;
                 case 'b': printf("1011"); break;
                 case 'c': printf("1100"); break;
                 case 'd': printf("1101"); break;
                 case 'e': printf("1110"); break;
                 case 'f': printf("1111"); break;
                 default:  printf("\nInvalid hexadecimal digit %c ",hexaDecimal[i]); return 0;
             }
             i++;
        }
    
    
        return 0;
    }
    So this is my current code, is there anyway I can reduce the size and use a main function to ask for input and a call function to do all the conversion and return it? I am confused for the past few days trying to figure it out and finally ended up here. Anyway can I write it as a something like this
    Code:
    int main()
    {
    //ask for user input hexadecimal into here and call a let's say hex2binary() function
    }
    
    int hex2binary(...)
    {
    //an array with dynamic memory, malloc? and convert it and return values
    }
    Any suggestion appreciated, I don't really need the full code, just a simple instruction on how and where to start. Thanks!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    333
    You want to get into the habit of specifying clean, portable, reusable, modular functions.

    For instance you don't want a function that converts between hex and binary, you want a function that takes an integer 0-255 and return s its 8-bit binary representation as ASCII. Then you want another function that converts from hex to an integer.

    In fact the fucntion that converts from hex to an integer already exists. it's called strtol(). You can also use the scanf() family for functions. You might want to write a hex-only strtol() as a learning exercise.

    So write the function inttobin(). You'll have to decide whether to hardcode 8 bits or pass in the number of bits you want, what to do with out of range input, etc.

    Then just plug it together in main().
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new to C - decimal to hexadecimal converter
    By emblem4ever in forum C Programming
    Replies: 20
    Last Post: 01-23-2010, 03:08 PM
  2. Characters to binary... hexadecimal to binary
    By Trauts in forum C++ Programming
    Replies: 48
    Last Post: 10-27-2002, 04:03 PM
  3. Binary/Hexadecimal
    By Trauts in forum C++ Programming
    Replies: 24
    Last Post: 09-13-2002, 11:48 PM
  4. Hexadecimal and Binary
    By Yaj in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-15-2002, 11:41 PM
  5. Binary,Hexadecimal to Text
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 05-11-2002, 07:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21