Results 1 to 15 of 15

Thread: Hex to Float

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    24

    Hex to Float

    How can I convert a string varaible containing the hex value for a floating point number into a floating point variable.

    Example:

    $hex = "45871b9c";
    $float = somefunction($hex);
    echo "$float";


    And I should get 4323.4512.

    Thanks.

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I thought maybe the hexdec($hex) function would work, but it does not give me 4323.4512...
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    24
    It converts it to an integer. I need float.

  4. #4
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    actually I don't think there is anything below php version 4.2 that will help you. if you have 4.2 or greater then you can try floatval()

    also explain to me how 45871B9C is 4323.4512 becasue hen I do it I get 1166482332

    also explain to me how a hex has a decimal?? not possible

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    24
    Well I'm using it to get info from a server and it sends back the binary data (a 4 character/byte string) of a 32 bit float which I'm able to convert into hex. I'll attach a base converter so you can see what I mean.
    Attached Files Attached Files

  6. #6
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    well I don't know how that program is doing it but you cannot get a hex to have a decimal. it is always rounded up to the next one if over 5 or 7 can't remember.

    but you could get creative about php to do what you want. other than that php doesn't have a built in function for this. if you had the code for that little program then maybe you can convert it to php. I don't even know how they are changing the number to be a float

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    24
    That hex number is the exact data in ram for a 32 bit float. In C++ for example you would just use the memcpy function and copy the int's data into the float variable.

  8. #8
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    exactly, how can ram have a decimal if it is off or on. hex is rounded, unless you are getting it from a program instead of RAM.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    24
    Well a float has to be stored somehow right? Well it get stored in ram as 32 bits of data. It's not stored in two pieces like a number before and after a decimal. Depending on the bits the computer determines if it's negative and what is before and after the decimal.

    More info on what I'm doing:
    Server responds with the following packet:
    (int32) -1
    (byte) ASCII 'D' (players response, S2A_PLAYER)
    (byte) active client count

    for each active client
    (byte) client number / index
    (string) player name
    (int32) client's frag total
    (float32) client's total time in-game

    I'm trying to get that 32 bit float into an int with php. Easy to do in VB or C++ but I'm having trouble with php.

  10. #10
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    that is very interesting.

    but I have to tell you, when you put a float into hex it willl round up or down depending on what the .10th spot is.

    how is the time getting entered in ram? because you might be able to save it to a flat file then read it with php.

    did you check the floactval() function?

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    24
    The float comes from a game server. The game server was made with C++ and sends the data over a winsock connection. The data comes in string form which I'm able to convert to hex but not into float. I haven't tried that file thing and may be worth trying if theres no functions that will do this. Is there a way to write a C++ plugin or module of something for php that will create a function to do this?

  12. #12
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    you migth be able to make a dll and use COM from php to access it.

    what does the string look like?

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    24
    The string looks something like:
    E‡œ

    Also, how would I go about making a dll for php and using COM?

  14. #14
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    sorry I can't read that string, what language is that?

    also you mentioned tha tis could be done in C++ so I assumed that you knew C and so you can make a dll in that language or VB, which ever. I have no idea how to make one, I just mentioned it.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    24
    That string is the hex data converted into ASCII form. I could code the dll if I knew what exports would have to be used or maybe see a description of the COM function and possible be able to figure it out from there.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width