Thread: Printing Hex as Binary

  1. #1
    UCF Mystic_Skies's Avatar
    Join Date
    Oct 2004
    Posts
    33

    Question Printing Hex as Binary

    I'm doing a fairly large program that acts somewhat as a compiler. I've gotten to a part where I cannot seem to find out how to take the two character object code indexes (in hex) and print it out in binary form. Anyone have any ideas to what functions handle this?

    Some of the code:
    Code:
    byte opcode[4];  /* each index will contain a two digit hex code */
    The indexes will be each "Objcode in Hex" and output is to the right:
    Code:
         Line   Objcode in Hex   Objcode in Binary
           1
           2
           3     B4 10           10110100 00010000
           4     A0 04           10100000 00000100
           5     33 20 06        00110011 00100000 00000110
           6     57 90 00 00     01010111 10010000 00000000 00000000
           7     B4 10           10110100 00010000
           8     4F 00 00        01001111 00000000 00000000
           9     00 00 00        00000000 00000000 00000000

  2. #2
    Hello,

    Each hex digit is 4 binary digits 0=0000, 1=0001, 2=0010, 3=0011 ... up to F=1111. Just take each hex digit from left to right and write the binary equivalents from left to right. You can drop any 0s that end up on the left.

    Here is a list of Hex and Binary values:
    Code:
    Decimal	Hex	Binary
    0	0	0000
    1 	1 	0001
    2 	2 	0010
    3 	3 	0011
    4 	4 	0100
    5 	5 	0101
    6 	6 	0110
    7 	7 	0111
    8 	8 	1000
    9 	9 	1001
    10 	a 	1010
    11 	b 	1011
    12 	c 	1100
    13 	d 	1101
    14 	e 	1110
    15 	f 	1111
    With this in mind, the hex number 8cf is 100011001111 in binary.

    Edit: Most computers now use "2's complement" arithmetic with negative numbers. One way to solve your problem is to convert Hex 'B' to Decimal '11' then to Binary '1011'.


    - Stack Overflow
    Last edited by Stack Overflow; 11-22-2004 at 02:29 PM. Reason: 2's compliment addition.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    UCF Mystic_Skies's Avatar
    Join Date
    Oct 2004
    Posts
    33
    Hello... I know how to convert hex to binary, but what I was asking is should I keep an array that represents what each 15 digits will be in binary and keep calling back to the array? Such as:

    Code:
    char codes[16];
    
    /* Decimal   Binary */
    codes[0] = 0000
    codes[1] = 0001
    codes[2] = 0010
    codes[3] = 0011
    codes[4] = 0100
    codes[5] = 0101
    codes[6] = 0110
    codes[7] = 0111
    codes[8] = 1000
    codes[9] = 1001
    codes[10] = 1010
    codes[11] = 1011
    codes[12] = 1100
    codes[13] = 1101
    codes[14] = 1110
    codes[15] = 1111

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    How about something like this?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    
    void foo(size_t line, const char *text)
    {
       char *end;
       printf("%3lu  %-12s  ", (long unsigned)line, text);
       do
       {
          unsigned char bit, result = strtoul(text, &end, 16);
          text = end;
          for ( bit = 1 << (CHAR_BIT - 1); bit; bit >>= 1 )
          {
             putchar(result & bit ? '1' : '0');
          }
          putchar(' ');
       } while ( *end != '\0' );
       putchar('\n');
    }
    
    int main ( void )
    {
       char *line[] =
       {
          "B4 10",
          "A0 04",
          "33 20 06",
          "57 90 00 00",
          "B4 10",
          "4F 00 00",
          "00 00 00",
       };
       size_t i;
       for ( i = 0; i < sizeof line / sizeof *line; ++i )
       {
          foo(i + 1, line[i]);
       }
       return 0;
    }
    
    /* my output
      1  B4 10         10110100 00010000
      2  A0 04         10100000 00000100
      3  33 20 06      00110011 00100000 00000110
      4  57 90 00 00   01010111 10010000 00000000 00000000
      5  B4 10         10110100 00010000
      6  4F 00 00      01001111 00000000 00000000
      7  00 00 00      00000000 00000000 00000000
    */
    Last edited by Dave_Sinkula; 11-22-2004 at 02:32 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Quote Originally Posted by Mystic_Skies
    should I keep an array that represents what each 15 digits will be in binary and keep calling back to the array?
    » It is not recommended from my point of view. There is a simple way to convert a decimal to binary. Here is the logic of it:

    Code:
    Start a loop that happens 4 or CHAR_BIT times [say i]
    	If 1 left shift of i is bitwise AND (&) of variable, then print 1
    	Else print 0
    Q. What is the bitwise AND operator?
    A. It is a two-place operator. It returns a word with each bit set only if the corresponding bit is set in both operands.

    For example:
    Code:
    x = 12;
    y = 10;
    z = x & y;
    Would result in:
    Code:
    	0000 1100
    &	0000 1010
    	---------
    	0000 1000 = 8 (decimal)
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can do it this way too:
    Code:
    itsme@itsme:~/C$ cat num2.c
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char *numstr = "37D95B";
      char hex[] = "0123456789ABCDEF";
      char *bin[] = { "0000", "0001", "0010", "0011", "0100", "0101",
                      "0110", "0111", "1000", "1001", "1010", "1011",
                      "1100", "1101", "1110", "1111" };
    
      while(*numstr)
      {
        printf("%s", bin[strchr(hex, *numstr)-hex]);
        numstr++;
      }
      putchar('\n');
    
      return 0;
    }
    itsme@itsme:~/C$ ./num2
    001101111101100101011011
    If you understand what you're doing, you're not learning anything.

  7. #7
    UCF Mystic_Skies's Avatar
    Join Date
    Oct 2004
    Posts
    33
    Thanks everyone for your great replies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  2. Hex to binary conversion and storing in nybble
    By shoobsie in forum C Programming
    Replies: 14
    Last Post: 10-25-2005, 02:51 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Is binary HEX?
    By Budgiekarl in forum Tech Board
    Replies: 11
    Last Post: 11-23-2003, 09:02 AM