Like Tree4Likes
  • 2 Post By CommonTater
  • 1 Post By CommonTater
  • 1 Post By tabstop

hex to binary

This is a discussion on hex to binary within the C Programming forums, part of the General Programming Boards category; Hi I am looking for a way of converting a hex string into binary, or at least somehow separating the ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    30

    hex to binary

    Hi

    I am looking for a way of converting a hex string into binary, or at least somehow separating the bitfields using masking, shift operators or some other method. I came up with a way of manually replacing each character with its known bits like 'a' with '1010', but just found it wasn't very good.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    Quote Originally Posted by nasser
    I came up with a way of manually replacing each character with its known bits like 'a' with '1010', but just found it wasn't very good.
    It would be good if you showed us this code and explained in what way you "found it wasn't very good".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    By "binary" do you mean a string containing the binary values or displaying it as a binary string?

    Your method of replacing characters with their binary equivalents is actually about the best way (unless you did it the hard way).

    Post your code, lets see what we can do...
    laserlight and Avenger625 like this.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    What you've described to be "not very good", I would find to be very good. Pretty darn efficient in fact I imagine.
    Do you want us to find a worse method or are you going to post the code?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    30
    The problem here is that the character array binary gets overloaded because I am reading in multiple (unknown) number of hex numbers.
    I also have the problem of not knowing how many inputs there are so I don't know how to define the array.
    Anyway I can circumvent these problems?

    Code:
    	while (scanf("%s\n", hex) == 1) {
    
          for (i=0; i<8; i++) {
    				
    							
    			if (hex[i] == '0') {
    					
    			    strcat(binary, "0000");
    				printf("\nIt is a 0");
    				
    				}
    			if (hex[i] == '1') {
    				
    				strcat(binary, "0001");
    				printf("\nIt is a 1");
    			}
    			if (hex[i] == '2') {
    				
    				strcat(binary, "0020");
    				printf("\nIt is a 2");
    			}
    			if (hex[i] == '3') {
    				
    				strcat(binary, "0011");
    				printf("\nIt is a 3");
    			}
    			if (hex[i] == '4') {
    				
    				strcat(binary, "0100");
    				printf("\nIt is a 4");
    			}
    			if (hex[i] == '5') {
    				
    				strcat(binary, "0101");
    				printf("\nIt is a 5");
    			}
    			if (hex[i] == '6') {
    				
    				strcat(binary, "0110");
    				printf("\nIt is a 6");
    			}
    			if (hex[i] == '7') {
    				
    				strcat(binary, "0111");
    				printf("\nIt is a 7");
    			}
    			if (hex[i] == '8') {
    				
    				strcat(binary, "1000");
    				printf("\nIt is a 8");
    			}
    			if (hex[i] == '9') {
    				
    				strcat(binary, "1001");
    				printf("\nIt is a 9");
    			}
    			if (hex[i] == 'a') {
    				
    				strcat(binary, "1010");
    				printf("\nIt is a 10");
    			}
    			if (hex[i] == 'b') {
    				
    				strcat(binary, "1011");
    				printf("\nIt is a 11");
    			}
    			if (hex[i] == 'c') {
    				
    				strcat(binary, "1100");
    				printf("\nIt is a 12");
    			}
    			if hex[i] == 'd') {
    				
    				strcat(binary, "1101");
    				printf("\nIt is a 13");
    			}
    			if (hex[i] == 'e') {
    				
    				strcat(binary, "1110");
    				printf("\nIt is a 14");
    			}
    			if (hex[i] == 'f') {
    				
    				strcat(binary, "1111");
    				printf("\nIt is a 15");
    			}
    			
    			
    		} //end for
    
    } //end while
    Last edited by nasser; 09-11-2011 at 02:42 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    strcat(binary, "0020");
    I don't think any of us were expecting that.

    If you want a string representation, then I don't see much better (other than perhaps using a switch to make it look a little cleaner). (EDIT: Or an array, as below.)
    Last edited by tabstop; 09-11-2011 at 02:49 PM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yep, you did it the hard way...
    Code:
    char hex[16];
    char binary[128] = {0};
    char convert[][] = {"0000","0001","0010","0011","0100","0101","0110","0111",
                        "1000","1001","1010","1011","1100","1101","1110","1111"};
    int i; 
    
    // read the hex input as a string up to 16 characters here
    
    // convert
    for (i = 0; i < strlen(hex); i++);
      { 
        if (isdigit(hex[i])
          strcat(binary,convert[hex[i]-'0']);
        else
          strcat(binary,convert[hex[i]-'A']);
       }
    This wants fleshing out with come code to make sure the number is a valid hex number (i.e. contains only 0..9 A..F) and is all caps, but it should work.
    tabstop likes this.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    30
    ^Thanks for that.

    But can you see I will have the problem where the binary string will be overloaded after a few inputs?
    The binary doesn't have to be a character string btw. I want to later separate the bits because these binary digits are machine codes for assembly instructions...
    Last edited by nasser; 09-11-2011 at 02:53 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by nasser View Post
    But can you see I will have the problem where the binary string will be overloaded after a few inputs?
    No. (I'm assuming you initialize binary somewhere at the top of the function.)

    And if you don't want it as a string, then you can just store it as a number (read it in with %x) and you're done.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    30
    Why won't it be overloaded? I actually tried it and it does.

    You defined binary as binary[128], each hex number takes 32 bits/elements, so after a few inputs it will fill up the array.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nasser View Post
    ^Thanks for that.

    But can you see I will have the problem where the binary string will be overloaded after a few inputs?
    The binary doesn't have to be a character string btw. I want to later separate the bits because these binary digits are machine codes for assembly instructions...
    Well... you could always do this... char *Binary = malloc(4294967280 * sizeof(char));... in which case overrunning the binary buffer would be the least of our worries...


    Why won't it be overloaded? I actually tried it and it does.
    You defined binary as binary[128], each hex number takes 32 bits, so after a few inputs it will fill up the array.
    And you can't figure out to clear the buffer before you load the next value?

    Text-Binary is useless for anything but display. Your computer won't have the first clue what to do with it... Now if you were reading the hexcode and converting it to machine-binary, using fscanf("%x"... you might actually stand a chance of turning text-hex into machine code...

    EDIT: Get out your dictionary and look up "Example"....
    Last edited by CommonTater; 09-11-2011 at 03:07 PM.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by nasser View Post
    Why won't it be overloaded? I actually tried it and it does.

    You defined binary as binary[128], each hex number takes 32 bits/elements, so after a few inputs it will fill up the array.
    So my assumption that you were bothering to initialize your variable was wrong. Once you get to 32 (or 64, or whatever your size is), you should be done and the next one needs to be starting from scratch.

    But if you just store it as a number, then you won't have to bother.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    30
    I initialised it like:
    Code:
     char binary[50] = {0}
    at the start of main. Is this right?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    For the first time you use it. But you need to initialize it every time you want to use it, not just once.
    iMalc likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read binary, change a value, write to binary
    By mammoth in forum C Programming
    Replies: 16
    Last Post: 05-12-2011, 10:56 AM
  2. Converting From Binary Tree to Threaded Binary Trees
    By elton_fan in forum C Programming
    Replies: 15
    Last Post: 11-08-2007, 10:41 PM
  3. Replies: 3
    Last Post: 11-25-2006, 03:14 PM
  4. Replies: 0
    Last Post: 11-04-2006, 10:07 AM
  5. Characters to binary... hexadecimal to binary
    By Trauts in forum C++ Programming
    Replies: 48
    Last Post: 10-27-2002, 04:03 PM

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