Thread: Convert 10.2 to binary

  1. #16
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    Thanks sooooooooo much for the help. It worked I know where to come when I need help. Thanks again to all of you. Is it possible that this can be broken into a function.
    Last edited by sara.stanley; 02-07-2006 at 12:00 PM.

  2. #17
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    I got it to work as a function thanks.

  3. #18
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    B = 2^PREC * N by jumpjack

    this formula can be used to print floating numbers in binary code to PREC bits

    in this post i will explain how it works

    similar to the bitwise operator << which shifts bits to the left. when you multiply a number by 2^PREC what happens is each bit shifts towards the left by PREC positions. below is a few examples.

    the binary code for 0.75 is 0000.1100 if multiplied by say 2^1 the answer will be 1.5. 1.5 has a binary code of 0001.1000. there you can see that all the bits have been shifted by 1 position to the left.

    0.75 multiplied by 2^2 is 3, which has the binary code 0011.0000. you can see the bits have been moved by 2 positions to the left.

    0.75 * 2^3 would shift bits 3 positions to the left, *2^4 it will shift bits by 4 positions to the left and so on.

    You can also work out what PREC should be used, in the example above it was 2. basically 2^PREC * N should give a whole number. that whole number should be as small as possible.

    here is how to get PREC for 0.875
    2^1*0.875 = 1.75
    2^2*0.875 = 3.5
    2^3*0.875 = 7
    2^4*0.875 = 14

    the smallest whole number we get is 7, the PREC we should use is 3 (in your program you could use a loop to find PREC)
    to find the equivalent binary code of 0.875 you would find the binary code for 7.

    hope this has been useful

  4. #19
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    I am trying to make this code work for hexadecimal also but I keep getting Segmentation Errors. I am adding the following conditions.

    Code:
    int c,i;
      printf("Its hexadecimal equivalent is  ");
      for (i=c;i>=0;--i)
      {
       if (num==10)
    	printf("A");
       else if (num==11)
    	printf("B");
       else if (num==12)
    	printf("C");
       else if (num==13)
    	printf("D");
       else if (num==14)
    	printf("E");
       else if (num==15)
    	printf("F");
       else
    	printf("%f",num);
      }
      return;
     }

  5. #20
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Maybe
    Code:
    	printf("%d",num);
    EDIT: just assuming num is int because one shouldn't compare floats for equality.
    Last edited by ZuK; 02-08-2006 at 04:06 AM.

  6. #21
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Changing the code to print in hexadecimal isn't hard since binary have pretty good relationship to it; 2^4 == 16
    So what needs to be done is changing the code so that it processes 4 bits at a time instead of 1 bit. This will give results in the range of 0-15 so I made another function which translates that into the char values '0' to '9' and 'A' to 'F'

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void dectohex (float num);
    char gethexchar (int n);
    
    int main (int argc, char** argv)
    {
    	float num;
    
    	if (argc >= 2) num = atof(argv[1]);
    	else scanf("%f", &num);
    
    	dectohex(num);
    
    	return 0;
    }
    
    void dectohex (float num)
    {
    	               int int_part;
    	      unsigned int i, pos = sizeof(int)*8 - 4;
    	const unsigned int int_msb = 0xF << (sizeof(int)*8 - 4);
    	//Even better pyramid.
    
    	int_part = num;
    	num -= int_part;
    	if (int_part < 0) {
    		putc('-', stdout);
    		int_part = -int_part;
    		num = -num;
    	}
    	putc('0', stdout); putc('x', stdout); //puts() is a jerk and prints a newline
    
    	for (i = 0; pos; i += 4, pos -= 4) {
    		if (int_part & (int_msb >> i))
    			break;
    	}
    	for (; pos; pos -= 4) {
    		putc(gethexchar(((int_part & (0xF << pos)) >> pos)), stdout);
    	}
    	putc(gethexchar(int_part & 0xF), stdout);
    
    	if (num)
    		putc('.', stdout);
    
    	while (num) {
    		num *= 16;
    		putc(gethexchar((int) num & 0xF), stdout);
    		num -= (int) num;
    	}
    }
    
    char gethexchar (int n)
    {
    	if (n > 9)
    		return 'A' - 10 + n;
    	return '0' + n;
    }
    PS: I am sorry for my use of magic numbers but I am a jerk and I don't feel like making the code look more messy than it already is.
    Last edited by OnionKnight; 02-08-2006 at 08:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  2. 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
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. convert word to binary
    By Supra in forum C Programming
    Replies: 13
    Last Post: 03-31-2002, 12:39 PM
  5. Convert a text file to a binary file
    By Cyber Kitten in forum C Programming
    Replies: 16
    Last Post: 02-04-2002, 08:53 AM