#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    31
    Rep Power
    11

    C : How to output bit representation in hexadecimal?


    How do I write a program in C that accepts a floating-point input and output it's bits representation in hexadecimal?

    Pls help.
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,075
    Rep Power
    1802
    Take a pointer to the float, cast it to a char pointer and print each character as a two digit hex value. Something like this.

    PHP Code:
    void float_to_hexfloat val )
    {
        
    charfloat_byte_array = (char*)&val ;
        
    size_t i ;
        
    printf"%d\n",sizeof(float) );
        
        
    printf"0x" ) ;
        for( 
    0sizeof( float ); i++ )
        {
            
    printf"%2.2X"float_byte_array[i] ) ;
        }

    Change float to double as necessary.

    Clifford
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    31
    Rep Power
    11
    Originally posted by clifford
    Take a pointer to the float, cast it to a char pointer and print each character as a two digit hex value. Something like this.

    PHP Code:
    void float_to_hexfloat val )
    {
        
    charfloat_byte_array = (char*)&val ;
        
    size_t i ;
        
    printf"%d\n",sizeof(float) );
        
        
    printf"0x" ) ;
        for( 
    0sizeof( float ); i++ )
        {
            
    printf"%2.2X"float_byte_array[i] ) ;
        }

    Change float to double as necessary.

    Clifford

    Thank you very much for the speedy reply, sir.

    It is running perfectly fine!

    May I know what is "%x" and "size_t i"?
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally posted by poppers
    Thank you very much for the speedy reply, sir.

    It is running perfectly fine!

    May I know what is "%x" and "size_t i"?
    size_t is the defined type that is returned by sizeof. It normally translates to int or short int.

    %x is the format descriptor of hexadecimal. All numbers are stored internally as binary. %d displays a number as decimal. %o as octal. %x as hex (unsigned is implied). Read the man page or help file on printf for more details.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    31
    Rep Power
    11
    Originally posted by dwise1_aol
    size_t is the defined type that is returned by sizeof. It normally translates to int or short int.

    %x is the format descriptor of hexadecimal. All numbers are stored internally as binary. %d displays a number as decimal. %o as octal. %x as hex (unsigned is implied). Read the man page or help file on printf for more details.
    Thank you, kind sir.

    Do u happen to know assembly too?
  10. #6
  11. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    did someone say asm? :D sorry to interject but i love assembly programming; what are you trying to do?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    31
    Rep Power
    11
    Originally posted by infamous41md
    did someone say asm? :D sorry to interject but i love assembly programming; what are you trying to do?

    Oh cool!!!

    Your reply is god-sent.....
    Both has to be written in MIPS

    1) I need to perform an addition for 2 64bit integer using 32bit registers.

    2) Multiply a small value by 5times using shift and add while ignoring overflow.

    Thanx a milion!!!
  14. #8
  15. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    hmm smells like someone is cooking some fresh homework on the grill and i just ate some pizza; sorry ;) . also i am unfamiliar with MIPS, but i am reading some stuff i found on google directly related to your assignment.... so happy googling :D
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    31
    Rep Power
    11
    thanx anyhow!
  18. #10
  19. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,075
    Rep Power
    1802
    Originally posted by dwise1_aol
    size_t is the defined type that is returned by sizeof. It normally translates to int or short int.
    In GCC it is "unsigned int". Originally I declared i as int and got a signed/unsigned comparison warning for the 'for' expression, so I changed type of i to agree with the result of sizeof.

    Clifford.
  20. #11
  21. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,075
    Rep Power
    1802
    Originally posted by poppers
    ...May I know what is "%x"...?
    dwise1_aol already answered this, however note that my code used "%X" rather than "%x", so that the digits A...F are displayed in uppercase rather than lowercase - choose whichever you prefer.

    Clifford.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    31
    Rep Power
    11
    Originally posted by clifford
    dwise1_aol already answered this, however note that my code used "%X" rather than "%x", so that the digits A...F are displayed in uppercase rather than lowercase - choose whichever you prefer.

    Clifford.

    Thank you very much for following it up.

    The detail explanation of usage of size_t & int is very useful to me.

    I appreciate it much.

IMN logo majestic logo threadwatch logo seochat tools logo