Thread: Convert float to hex then to int then to hex and last convert back to hex

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    30

    Convert float to hex then to int then to hex and last convert back to hex

    I want to do the conversion for the following type mention above. I am stuck of doing it. Is anyone have any idea of doing the conversion above? Thank you very much.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You're mixing concepts. Int and float are c data types. hex is a display format. How a number is stored is quite separate from how a number is displayed. What do you actually want to be changing?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    Quote Originally Posted by King Mir View Post
    You're mixing concepts. Int and float are c data types. hex is a display format. How a number is stored is quite separate from how a number is displayed. What do you actually want to be changing?
    Then if I change the display of float to int then will I able to get change back for the float display?

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by zhengkoon8 View Post
    Then if I change the display of float to int then will I able to get change back for the float display?
    int and float are two different data types not display formats.

    You can cast a float to an int which will throw away the fractional part (e.g. 12.34 will become 12) but going back from the int to float won't add it again.
    But as long as you don't change your float variable that's not a problem.

    Can you give an example code of what you want to achieve?

    Bye, Andreas

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Assuming you mean a hex string, you still need to specify how you want to convert it.

    It's like saying you want to convert a circle to a square. We don't know if you want to cut round pieces off it, or add on some flat edges, or squash it into shape, and clearly the results are not the same.

    I can think of more than two ways to convert a float to a hex string.

    You also wont get any code from us without showing what you have tried.
    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"

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I don't think anyone can help before we know what you want to see.

    Suppose you had a program that worked. What kind of output is there?

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    Quote Originally Posted by AndiPersti View Post
    int and float are two different data types not display formats.

    You can cast a float to an int which will throw away the fractional part (e.g. 12.34 will become 12) but going back from the int to float won't add it again.
    But as long as you don't change your float variable that's not a problem.

    Can you give an example code of what you want to achieve?

    Bye, Andreas

    I have done casting. and it work. but the casting cannot retrieve the value or decimal point onward when cast from integer back to float right? Do you need this code?

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    Quote Originally Posted by iMalc View Post
    Assuming you mean a hex string, you still need to specify how you want to convert it.

    It's like saying you want to convert a circle to a square. We don't know if you want to cut round pieces off it, or add on some flat edges, or squash it into shape, and clearly the results are not the same.

    I can think of more than two ways to convert a float to a hex string.

    You also wont get any code from us without showing what you have tried.
    I have not write the program for hex. Still in process. but the expected output should be like something like this

    output
    float 1022.12
    hex 03FE.1EB8
    int 1022
    hex 03FE
    float 1022.12

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    Quote Originally Posted by whiteflags View Post
    I don't think anyone can help before we know what you want to see.

    Suppose you had a program that worked. What kind of output is there?
    I have not write the program for hex. Still in process. but the expected output should be like something like this

    output
    float 1022.12
    hex 03FE.1EB8
    int 1022
    hex 03FE
    float 1022.12

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think I know what function you need.

    modf - Math.h - C - C++ Computing Reference with Worked Examples

    Now, you will need to do some additional math before you convert the fractional portion to hex, but I think you know how to make integers hex, so you should be able to figure it out.

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by zhengkoon8 View Post
    I have done casting. and it work. but the casting cannot retrieve the value or decimal point onward when cast from integer back to float right?
    Correct, casting float to int just works in one direction because you loose information (the fractional part)

    Bye, Andreas

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by zhengkoon8 View Post
    I have not write the program for hex. Still in process. but the expected output should be like something like this

    output
    float 1022.12
    hex 03FE.1EB8
    int 1022
    hex 03FE
    float 1022.12
    Your first and last line are the same. Thus you don't need to convert the int back to float. Just print the original float.

    Bye, Andreas

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    C doesn't have a built in function to print floating point numbers in hex. You'll have to write one yourself. For the rest, you can use printf.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by King Mir View Post
    C doesn't have a built in function to print floating point numbers in hex. You'll have to write one yourself. For the rest, you can use printf.
    Sure it does--the 'a' and 'A' conversion specifiers print floating point numbers in hexadecimal (at least in C99 and later). printf(3): formatted output conversion - Linux man page

    Example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            printf("%a\n", 3.1415927);
            return 0;
    }
    Output:
    Code:
    0x1.921fb5a7ed197p+1

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by christop View Post
    Sure it does--the 'a' and 'A' conversion specifiers print floating point numbers in hexadecimal (at least in C99 and later). printf(3): formatted output conversion - Linux man page

    Example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            printf("%a\n", 3.1415927);
            return 0;
    }
    Output:
    Code:
    0x1.921fb5a7ed197p+1
    Neat.

    So the OP can use one variable and printf to display all the formats. No need for fmod either, just use print's precision specifier to make it round the output to integers.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert a.out back to C code??
    By Rob4226 in forum C Programming
    Replies: 3
    Last Post: 09-16-2009, 10:33 PM
  2. convert exe back to c source
    By muhammadwaqas in forum C Programming
    Replies: 9
    Last Post: 04-19-2008, 03:05 PM
  3. Convert hex back to int?
    By ryeguy in forum C++ Programming
    Replies: 2
    Last Post: 01-15-2008, 03:01 AM
  4. Replies: 8
    Last Post: 07-08-2005, 09:12 AM
  5. Unresolved external 'convert(float, float)'
    By Ipsec Espah in forum C++ Programming
    Replies: 4
    Last Post: 05-21-2003, 10:08 AM