Thread: divide 2 hex numbers

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    2

    divide 2 hex numbers

    hi

    I have to write a C program that divides 2 hex numbers to get a number with digits behind the comma. like 6644/CC88 = 0,5(decimal)

    I heard something about fixed point notation but I can't get it the right way.

    Can anyone help me on this?

    Thanks
    Dust

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    What is the actual question? You can read in hexadecimal numbers with scanf(). For printing out floating point values, you can set the amount of decimal places by adjusting the %f to reflect what you want. I believe you can do something like %.2f or whatever to get 2 decimal places.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Something like this?
    Code:
    #include <stdio.h>
    
    int main()
    {
       unsigned int a = 0x6644, b = 0xCC88;
       double result = (double)a / b;
       printf("%X / %X = %g\n", a, b, result);
       return 0;
    } 
    
    /* my output
    6644 / CC88 = 0.5
    */
    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.*

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    2
    The thing is that the program needs to get on a chip with limited space. so double could be a problem. is there a way to do it with char's?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by dust555 View Post
    The thing is that the program needs to get on a chip with limited space. so double could be a problem. is there a way to do it with char's?
    Is it a proven bottleneck, or do you just want to reinvent the wheel?
    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.*

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I heard something about fixed point notation but I can't get it the right way.
    I've heard about them as well, and they work just fine.

    Maybe you should post your attempt.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Hex Editing help and information please...
    By SG57 in forum C Programming
    Replies: 9
    Last Post: 06-25-2006, 12:30 AM
  3. converting hex to dec
    By jibbles in forum C Programming
    Replies: 20
    Last Post: 08-07-2004, 11:40 PM
  4. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM